Hi, this is Shunchi!

  • Home

  • Tags0

  • Archives267

  • Categories0

  • Curricula

  • DSA

  • LeetCode_Notes

  • Interviews

  • General

  • Resume

78. Subsets

Posted on 2020-07-15 | Edited on 2021-01-22

LeetCode

Backtracking

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Solution {
public List<List<Integer>> subsets(int[] nums) {
List<List<Integer>> res= new ArrayList();
for(int k=0; k<=nums.length; k++){
subsets(res, new ArrayList<Integer>(), 0, k, nums);
}
return res;
}
public void subsets(List<List<Integer>> res, List<Integer> ls, int start, int k, int[] nums){
if(k==0){
res.add(new ArrayList<Integer>(ls));
return;
}
for(int i=start; i<nums.length; i++){
ls.add(nums[i]);
subsets(res,ls,i+1,k-1,nums);
ls.remove(ls.size()-1);
}
}
}

<1…495051…267>
ShunchiZhou

ShunchiZhou

267 posts
RSS
GitHub E-Mail Gitbook Linkedin
© 2024 ShunchiZhou
Powered by Hexo v5.4.0
|
0%