Skip to content

Commit c5df5c4

Browse files
committed
feat: add solutions to lc/lcof2 problem: Subsets
1 parent 3db198b commit c5df5c4

File tree

12 files changed

+373
-90
lines changed

12 files changed

+373
-90
lines changed

.github/workflows/compress.yml

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ on:
88
- "**.jpeg"
99
- "**.png"
1010
- "**.webp"
11+
workflow_dispatch:
1112

1213
jobs:
1314
compress:

lcof2/剑指 Offer II 079. 所有子集/README.md

+100-1
Original file line numberDiff line numberDiff line change
@@ -43,22 +43,121 @@
4343

4444
<!-- 这里可写通用的实现逻辑 -->
4545

46+
回溯法的基本模板:
47+
48+
```py
49+
res = []
50+
path = []
51+
52+
def backtrack(未探索区域, res, path):
53+
if path 满足条件:
54+
res.add(path) # 深度拷贝
55+
# return # 如果不用继续搜索需要 return
56+
for 选择 in 未探索区域当前可能的选择:
57+
if 当前选择符合要求:
58+
path.add(当前选择)
59+
backtrack(新的未探索区域, res, path)
60+
path.pop()
61+
```
62+
4663
<!-- tabs:start -->
4764

4865
### **Python3**
4966

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

5269
```python
53-
70+
class Solution:
71+
def subsets(self, nums: List[int]) -> List[List[int]]:
72+
res = []
73+
74+
def dfs(i, n, t):
75+
res.append(t.copy())
76+
if i == n:
77+
return
78+
for j in range(i, n):
79+
t.append(nums[j])
80+
dfs(j + 1, n, t)
81+
t.pop()
82+
83+
dfs(0, len(nums), [])
84+
return res
5485
```
5586

5687
### **Java**
5788

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

6091
```java
92+
class Solution {
93+
public List<List<Integer>> subsets(int[] nums) {
94+
List<List<Integer>> res = new ArrayList<>();
95+
dfs(0, nums, new ArrayList<>(), res);
96+
return res;
97+
}
98+
99+
private void dfs(int i, int[] nums, List<Integer> t, List<List<Integer>> res) {
100+
res.add(new ArrayList<>(t));
101+
if (i == nums.length) {
102+
return;
103+
}
104+
for (int j = i; j < nums.length; ++j) {
105+
t.add(nums[j]);
106+
dfs(j + 1, nums, t, res);
107+
t.remove(t.size() - 1);
108+
}
109+
}
110+
}
111+
```
112+
113+
### **C++**
114+
115+
```cpp
116+
class Solution {
117+
public:
118+
vector<vector<int>> subsets(vector<int>& nums) {
119+
vector<vector<int>> res;
120+
vector<int> t;
121+
dfs(0, nums, t, res);
122+
return res;
123+
}
124+
125+
void dfs(int i, vector<int>& nums, vector<int> t, vector<vector<int>>& res) {
126+
res.push_back(t);
127+
if (i == nums.size()) return;
128+
for (int j = i; j < nums.size(); ++j)
129+
{
130+
t.push_back(nums[j]);
131+
dfs(j + 1, nums, t, res);
132+
t.pop_back();
133+
}
134+
}
135+
};
136+
```
61137

138+
### **Go**
139+
140+
```go
141+
func subsets(nums []int) [][]int {
142+
var res [][]int
143+
var t []int
144+
dfs(0, nums, t, &res)
145+
return res
146+
}
147+
148+
func dfs(i int, nums, t []int, res *[][]int) {
149+
cp := make([]int, len(t))
150+
copy(cp, t)
151+
*res = append(*res, cp)
152+
if i == len(nums) {
153+
return
154+
}
155+
for j := i; j < len(nums); j++ {
156+
t = append(t, nums[j])
157+
dfs(j+1, nums, t, res)
158+
t = t[:len(t)-1]
159+
}
160+
}
62161
```
63162

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

solution/0000-0099/0078.Subsets/README.md

+72-19
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
<li><code>nums</code> 中的所有元素 <strong>互不相同</strong></li>
3737
</ul>
3838

39-
4039
## 解法
4140

4241
<!-- 这里可写通用的实现逻辑 -->
@@ -67,15 +66,18 @@ def backtrack(未探索区域, res, path):
6766
```python
6867
class Solution:
6968
def subsets(self, nums: List[int]) -> List[List[int]]:
70-
def dfs(nums, i, res, path):
71-
res.append(copy.deepcopy(path))
72-
while i < len(nums):
73-
path.append(nums[i])
74-
dfs(nums, i + 1, res, path)
75-
path.pop()
76-
i += 1
77-
res, path = [], []
78-
dfs(nums, 0, res, path)
69+
res = []
70+
71+
def dfs(i, n, t):
72+
res.append(t.copy())
73+
if i == n:
74+
return
75+
for j in range(i, n):
76+
t.append(nums[j])
77+
dfs(j + 1, n, t)
78+
t.pop()
79+
80+
dfs(0, len(nums), [])
7981
return res
8082
```
8183

@@ -86,24 +88,75 @@ class Solution:
8688
```java
8789
class Solution {
8890
public List<List<Integer>> subsets(int[] nums) {
89-
List<Integer> path = new ArrayList<>();
9091
List<List<Integer>> res = new ArrayList<>();
91-
dfs(nums, 0, res, path);
92+
dfs(0, nums, new ArrayList<>(), res);
9293
return res;
9394
}
9495

95-
private void dfs(int[] nums, int i, List<List<Integer>> res, List<Integer> path) {
96-
res.add(new ArrayList<>(path));
97-
while (i < nums.length) {
98-
path.add(nums[i]);
99-
dfs(nums, i + 1, res, path);
100-
path.remove(path.size() - 1);
101-
++i;
96+
private void dfs(int i, int[] nums, List<Integer> t, List<List<Integer>> res) {
97+
res.add(new ArrayList<>(t));
98+
if (i == nums.length) {
99+
return;
100+
}
101+
for (int j = i; j < nums.length; ++j) {
102+
t.add(nums[j]);
103+
dfs(j + 1, nums, t, res);
104+
t.remove(t.size() - 1);
102105
}
103106
}
104107
}
105108
```
106109

110+
### **C++**
111+
112+
```cpp
113+
class Solution {
114+
public:
115+
vector<vector<int>> subsets(vector<int>& nums) {
116+
vector<vector<int>> res;
117+
vector<int> t;
118+
dfs(0, nums, t, res);
119+
return res;
120+
}
121+
122+
void dfs(int i, vector<int>& nums, vector<int> t, vector<vector<int>>& res) {
123+
res.push_back(t);
124+
if (i == nums.size()) return;
125+
for (int j = i; j < nums.size(); ++j)
126+
{
127+
t.push_back(nums[j]);
128+
dfs(j + 1, nums, t, res);
129+
t.pop_back();
130+
}
131+
}
132+
};
133+
```
134+
135+
### **Go**
136+
137+
```go
138+
func subsets(nums []int) [][]int {
139+
var res [][]int
140+
var t []int
141+
dfs(0, nums, t, &res)
142+
return res
143+
}
144+
145+
func dfs(i int, nums, t []int, res *[][]int) {
146+
cp := make([]int, len(t))
147+
copy(cp, t)
148+
*res = append(*res, cp)
149+
if i == len(nums) {
150+
return
151+
}
152+
for j := i; j < len(nums); j++ {
153+
t = append(t, nums[j])
154+
dfs(j+1, nums, t, res)
155+
t = t[:len(t)-1]
156+
}
157+
}
158+
```
159+
107160
### **...**
108161

109162
```

0 commit comments

Comments
 (0)