Skip to content

Commit 7d1f9dd

Browse files
committed
feat: add solutions to lcof2 problem: No.079
No.剑指 Offer II 079. 所有子集
1 parent 387b96a commit 7d1f9dd

File tree

3 files changed

+74
-0
lines changed

3 files changed

+74
-0
lines changed

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

+42
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,48 @@ func dfs(i int, nums, t []int, res *[][]int) {
158158
}
159159
```
160160

161+
### **TypeScipt**
162+
163+
```ts
164+
function subsets(nums: number[]): number[][] {
165+
const n = nums.length;
166+
const ans = [];
167+
const dfs = (i: number, t: number[]) => {
168+
ans.push([...t]);
169+
while (i < n) {
170+
t.push(nums[i++]);
171+
dfs(i, t);
172+
t.pop();
173+
}
174+
};
175+
dfs(0, []);
176+
return ans;
177+
}
178+
```
179+
180+
### **Rust**
181+
182+
```rust
183+
impl Solution {
184+
fn dfs(mut i: usize, t: &mut Vec<i32>, ans: &mut Vec<Vec<i32>>, nums: &Vec<i32>) {
185+
ans.push(t.clone());
186+
while i < nums.len() {
187+
t.push(nums[i]);
188+
i += 1;
189+
Self::dfs(i, t, ans, nums);
190+
t.pop();
191+
}
192+
}
193+
194+
pub fn subsets(nums: Vec<i32>) -> Vec<Vec<i32>> {
195+
let mut ans = Vec::new();
196+
let mut t = Vec::new();
197+
Self::dfs(0, &mut t, &mut ans, &nums);
198+
ans
199+
}
200+
}
201+
```
202+
161203
### **...**
162204

163205
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
impl Solution {
2+
fn dfs(mut i: usize, t: &mut Vec<i32>, ans: &mut Vec<Vec<i32>>, nums: &Vec<i32>) {
3+
ans.push(t.clone());
4+
while i < nums.len() {
5+
t.push(nums[i]);
6+
i += 1;
7+
Self::dfs(i, t, ans, nums);
8+
t.pop();
9+
}
10+
}
11+
12+
pub fn subsets(nums: Vec<i32>) -> Vec<Vec<i32>> {
13+
let mut ans = Vec::new();
14+
let mut t = Vec::new();
15+
Self::dfs(0, &mut t, &mut ans, &nums);
16+
ans
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
function subsets(nums: number[]): number[][] {
2+
const n = nums.length;
3+
const ans = [];
4+
const dfs = (i: number, t: number[]) => {
5+
ans.push([...t]);
6+
while (i < n) {
7+
t.push(nums[i++]);
8+
dfs(i, t);
9+
t.pop();
10+
}
11+
};
12+
dfs(0, []);
13+
return ans;
14+
}

0 commit comments

Comments
 (0)