Guest User

Untitled

a guest
Mar 18th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  1. class Solution {
  2. public List<List<Integer>> subsets(int[] nums) {
  3. List<List<Integer>> res = new ArrayList<>();
  4. backtracking(res, new ArrayList<Integer>(), 0, nums);
  5. return res;
  6. }
  7.  
  8. // Each level it walks from start to end,
  9. // and recursively process the remainder
  10. // Becuase all numbers are unique, so each level number
  11. // cannot be the same, which ensures all results are unique
  12. private void backtracking(List<List<Integer>> res, List<Integer> path, int start, int[] nums) {
  13. res.add(new ArrayList<>(path));
  14.  
  15. for (int i = start; i < nums.length; i++) {
  16. // Make a new move
  17. path.add(nums[i]);
  18.  
  19. // Advance to next level
  20. backtracking(res, path, i + 1, nums);
  21.  
  22. // Undo move after backtracking from the level below
  23. path.remove(path.size() - 1);
  24. }
  25. }
  26. }
Add Comment
Please, Sign In to add comment