Skip to content

Instantly share code, notes, and snippets.

@forkercat
Created September 23, 2019 00:28
Show Gist options
  • Save forkercat/f466fd03ca56ad76ff07a4f0e22f9a72 to your computer and use it in GitHub Desktop.
Save forkercat/f466fd03ca56ad76ff07a4f0e22f9a72 to your computer and use it in GitHub Desktop.
public List<List<Integer>> subsets(int[] nums) {
List<List<Integer>> result = new ArrayList<>();
List<Integer> numList = new ArrayList<>();
result.add(new ArrayList<>()); // empty set
subsets(0, nums, numList, result);
return result;
}
private void subsets(int offset, int[] nums, List<Integer> numList, List<List<Integer>> result) {
if (offset >= nums.length) {
return;
}
int val = nums[offset];
// pick
numList.add(val);
subsets(offset + 1, nums, numList, result);
// add to result
result.add(new ArrayList<>(numList));
// not pick
numList.remove(numList.size() - 1);
subsets(offset + 1, nums, numList, result);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment