-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0077_combinations.java
More file actions
94 lines (80 loc) · 2.03 KB
/
0077_combinations.java
File metadata and controls
94 lines (80 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*
* https://leetcode-cn.com/problems/combinations/
给定两个整数 n 和 k,返回 1 ... n 中所有可能的 k 个数的组合。
示例:
输入: n = 4, k = 2
输出:
[
[2,4],
[3,4],
[2,3],
[1,2],
[1,3],
[1,4],
]
---------------------------------------------------------------------------------------------------
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n.
Example:
Input: n = 4, k = 2
Output:
[
[2,4],
[3,4],
[2,3],
[1,2],
[1,3],
[1,4],
]
*/
class MySolution {
public List<List<Integer>> combine(int n, int k) {
List<List<Integer>> combinations = new ArrayList<>();
if (n < 1 || k < 1 || n < k) return combinations;
backtrack(combinations, new LinkedList<>(), 1, n, k);
return combinations;
}
/**
*
* @param combinations 所有排列
* @param com 某种可能的组合
* @param i
* @param n
* @param k 组合中尚未确定的个数
*/
private void backtrack(List<List<Integer>> combinations, LinkedList<Integer> com,
int i, int n, int k) {
if (k == 0) {
combinations.add(new ArrayList<>(com));
return;
}
for (; i <= n - k + 1; i++) {
com.add(i);
backtrack(combinations, com, i + 1, n, k - 1);
com.pollLast();
}
}
}
class Solution1 {
List<List<Integer>> res = new ArrayList<>();
private int n;
private int k;
public List<List<Integer>> combine(int n, int k) {
this.n = n;
this.k = k;
if (n >= 1) {
helper(new ArrayList<>(), 1);
}
return res;
}
private void helper(List<Integer> list, int i) {
if (list.size() == k) {
res.add(new ArrayList<>(list));
return;
}
for (int j = i; j <= n - (k - list.size() - 1); j++) {
list.add(j);
helper(list, j + 1);
list.remove(list.size() - 1);
}
}
}