标签:remove length htm void com class array bin vat
与77. 组合几乎一样,我们只需将代码稍微改一下即可:
class Solution {
private void dfs(int n, int k, int last, int[] nums, List<Integer> cur, List<List<Integer>> ans) {
if (k == 0) {
ans.add(new ArrayList<>(cur));
return;
}
for (Integer i = last + 1; i <= n; i++) {
cur.add(nums[i - 1]);
dfs(n, k - 1, i, nums, cur, ans);
cur.remove(((Integer) nums[i - 1]));
}
}
public List<List<Integer>> subsets(int[] nums) {
List<List<Integer>> ans = new ArrayList<>();
List<Integer> cur = new ArrayList<>();
for (int k = 0; k <= nums.length; k++) {
dfs(nums.length, k, 0, nums, cur, ans);
}
return ans;
}
}
标签:remove length htm void com class array bin vat
原文地址:https://www.cnblogs.com/acbingo/p/9390102.html