Skip to content

Commit 3db198b

Browse files
committed
feat: add solutions to lc/lcof2 problem: Combinations
1 parent 10fa6c1 commit 3db198b

File tree

15 files changed

+449
-20
lines changed

15 files changed

+449
-20
lines changed

lcof2/剑指 Offer II 080. 含有 k 个元素的组合/README.md

+88-2
Original file line numberDiff line numberDiff line change
@@ -41,27 +41,113 @@
4141

4242
<p><meta charset="UTF-8" />注意:本题与主站 77&nbsp;题相同:&nbsp;<a href="https://leetcode-cn.com/problems/combinations/">https://leetcode-cn.com/problems/combinations/</a></p>
4343

44-
4544
## 解法
4645

4746
<!-- 这里可写通用的实现逻辑 -->
4847

48+
深度优先搜索 DFS。
49+
4950
<!-- tabs:start -->
5051

5152
### **Python3**
5253

5354
<!-- 这里可写当前语言的特殊实现逻辑 -->
5455

5556
```python
56-
57+
class Solution:
58+
def combine(self, n: int, k: int) -> List[List[int]]:
59+
res = []
60+
61+
def dfs(i, n, k, t):
62+
if len(t) == k:
63+
res.append(t.copy())
64+
return
65+
for j in range(i, n + 1):
66+
t.append(j)
67+
dfs(j + 1, n, k, t)
68+
t.pop()
69+
70+
dfs(1, n, k, [])
71+
return res
5772
```
5873

5974
### **Java**
6075

6176
<!-- 这里可写当前语言的特殊实现逻辑 -->
6277

6378
```java
79+
class Solution {
80+
public List<List<Integer>> combine(int n, int k) {
81+
List<List<Integer>> res = new ArrayList<>();
82+
dfs(1, n, k, new ArrayList<>(), res);
83+
return res;
84+
}
85+
86+
private void dfs(int i, int n, int k, List<Integer> t, List<List<Integer>> res) {
87+
if (t.size() == k) {
88+
res.add(new ArrayList<>(t));
89+
return;
90+
}
91+
for (int j = i; j <= n; ++j) {
92+
t.add(j);
93+
dfs(j + 1, n, k, t, res);
94+
t.remove(t.size() - 1);
95+
}
96+
}
97+
}
98+
```
99+
100+
### **C++**
101+
102+
```cpp
103+
class Solution {
104+
public:
105+
vector<vector<int>> combine(int n, int k) {
106+
vector<vector<int>> res;
107+
vector<int> t;
108+
dfs(1, n, k, t, res);
109+
return res;
110+
}
111+
112+
void dfs(int i, int n, int k, vector<int> t, vector<vector<int>>& res) {
113+
if (t.size() == k)
114+
{
115+
res.push_back(t);
116+
return;
117+
}
118+
for (int j = i; j <= n; ++j)
119+
{
120+
t.push_back(j);
121+
dfs(j + 1, n, k, t, res);
122+
t.pop_back();
123+
}
124+
}
125+
};
126+
```
64127

128+
### **Go**
129+
130+
```go
131+
func combine(n int, k int) [][]int {
132+
var res [][]int
133+
var t []int
134+
dfs(1, n, k, t, &res)
135+
return res
136+
}
137+
138+
func dfs(i, n, k int, t []int, res *[][]int) {
139+
if len(t) == k {
140+
cp := make([]int, k)
141+
copy(cp, t)
142+
*res = append(*res, cp)
143+
return
144+
}
145+
for j := i; j <= n; j++ {
146+
t = append(t, j)
147+
dfs(j+1, n, k, t, res)
148+
t = t[:len(t)-1]
149+
}
150+
}
65151
```
66152

67153
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public:
3+
vector<vector<int>> combine(int n, int k) {
4+
vector<vector<int>> res;
5+
vector<int> t;
6+
dfs(1, n, k, t, res);
7+
return res;
8+
}
9+
10+
void dfs(int i, int n, int k, vector<int> t, vector<vector<int>>& res) {
11+
if (t.size() == k)
12+
{
13+
res.push_back(t);
14+
return;
15+
}
16+
for (int j = i; j <= n; ++j)
17+
{
18+
t.push_back(j);
19+
dfs(j + 1, n, k, t, res);
20+
t.pop_back();
21+
}
22+
}
23+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
func combine(n int, k int) [][]int {
2+
var res [][]int
3+
var t []int
4+
dfs(1, n, k, t, &res)
5+
return res
6+
}
7+
8+
func dfs(i, n, k int, t []int, res *[][]int) {
9+
if len(t) == k {
10+
cp := make([]int, k)
11+
copy(cp, t)
12+
*res = append(*res, cp)
13+
return
14+
}
15+
for j := i; j <= n; j++ {
16+
t = append(t, j)
17+
dfs(j+1, n, k, t, res)
18+
t = t[:len(t)-1]
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public List<List<Integer>> combine(int n, int k) {
3+
List<List<Integer>> res = new ArrayList<>();
4+
dfs(1, n, k, new ArrayList<>(), res);
5+
return res;
6+
}
7+
8+
private void dfs(int i, int n, int k, List<Integer> t, List<List<Integer>> res) {
9+
if (t.size() == k) {
10+
res.add(new ArrayList<>(t));
11+
return;
12+
}
13+
for (int j = i; j <= n; ++j) {
14+
t.add(j);
15+
dfs(j + 1, n, k, t, res);
16+
t.remove(t.size() - 1);
17+
}
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution:
2+
def combine(self, n: int, k: int) -> List[List[int]]:
3+
res = []
4+
5+
def dfs(i, n, k, t):
6+
if len(t) == k:
7+
res.append(t.copy())
8+
return
9+
for j in range(i, n + 1):
10+
t.append(j)
11+
dfs(j + 1, n, k, t)
12+
t.pop()
13+
14+
dfs(1, n, k, [])
15+
return res

solution/0000-0099/0077.Combinations/README.md

+88-2
Original file line numberDiff line numberDiff line change
@@ -21,27 +21,113 @@
2121
[1,4],
2222
]</pre>
2323

24-
2524
## 解法
2625

2726
<!-- 这里可写通用的实现逻辑 -->
2827

28+
深度优先搜索 DFS。
29+
2930
<!-- tabs:start -->
3031

3132
### **Python3**
3233

3334
<!-- 这里可写当前语言的特殊实现逻辑 -->
3435

3536
```python
36-
37+
class Solution:
38+
def combine(self, n: int, k: int) -> List[List[int]]:
39+
res = []
40+
41+
def dfs(i, n, k, t):
42+
if len(t) == k:
43+
res.append(t.copy())
44+
return
45+
for j in range(i, n + 1):
46+
t.append(j)
47+
dfs(j + 1, n, k, t)
48+
t.pop()
49+
50+
dfs(1, n, k, [])
51+
return res
3752
```
3853

3954
### **Java**
4055

4156
<!-- 这里可写当前语言的特殊实现逻辑 -->
4257

4358
```java
59+
class Solution {
60+
public List<List<Integer>> combine(int n, int k) {
61+
List<List<Integer>> res = new ArrayList<>();
62+
dfs(1, n, k, new ArrayList<>(), res);
63+
return res;
64+
}
65+
66+
private void dfs(int i, int n, int k, List<Integer> t, List<List<Integer>> res) {
67+
if (t.size() == k) {
68+
res.add(new ArrayList<>(t));
69+
return;
70+
}
71+
for (int j = i; j <= n; ++j) {
72+
t.add(j);
73+
dfs(j + 1, n, k, t, res);
74+
t.remove(t.size() - 1);
75+
}
76+
}
77+
}
78+
```
79+
80+
### **C++**
81+
82+
```cpp
83+
class Solution {
84+
public:
85+
vector<vector<int>> combine(int n, int k) {
86+
vector<vector<int>> res;
87+
vector<int> t;
88+
dfs(1, n, k, t, res);
89+
return res;
90+
}
91+
92+
void dfs(int i, int n, int k, vector<int> t, vector<vector<int>>& res) {
93+
if (t.size() == k)
94+
{
95+
res.push_back(t);
96+
return;
97+
}
98+
for (int j = i; j <= n; ++j)
99+
{
100+
t.push_back(j);
101+
dfs(j + 1, n, k, t, res);
102+
t.pop_back();
103+
}
104+
}
105+
};
106+
```
44107

108+
### **Go**
109+
110+
```go
111+
func combine(n int, k int) [][]int {
112+
var res [][]int
113+
var t []int
114+
dfs(1, n, k, t, &res)
115+
return res
116+
}
117+
118+
func dfs(i, n, k int, t []int, res *[][]int) {
119+
if len(t) == k {
120+
cp := make([]int, k)
121+
copy(cp, t)
122+
*res = append(*res, cp)
123+
return
124+
}
125+
for j := i; j <= n; j++ {
126+
t = append(t, j)
127+
dfs(j+1, n, k, t, res)
128+
t = t[:len(t)-1]
129+
}
130+
}
45131
```
46132

47133
### **...**

0 commit comments

Comments
 (0)