Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public List<List<Integer>> subsets(int[] nums) {
- List<List<Integer>> res = new ArrayList<>();
- backtracking(res, new ArrayList<Integer>(), 0, nums);
- return res;
- }
- // Each level it walks from start to end,
- // and recursively process the remainder
- // Becuase all numbers are unique, so each level number
- // cannot be the same, which ensures all results are unique
- private void backtracking(List<List<Integer>> res, List<Integer> path, int start, int[] nums) {
- res.add(new ArrayList<>(path));
- for (int i = start; i < nums.length; i++) {
- // Make a new move
- path.add(nums[i]);
- // Advance to next level
- backtracking(res, path, i + 1, nums);
- // Undo move after backtracking from the level below
- path.remove(path.size() - 1);
- }
- }
- }
Add Comment
Please, Sign In to add comment