Created
September 23, 2019 00:28
-
-
Save forkercat/f466fd03ca56ad76ff07a4f0e22f9a72 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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