Leetcode 78. Subsets (backtracking) 90 subset
阿新 • • 發佈:2018-05-15
check emp rev ++ remove tin turn ins tracking
using prev
class Solution { List<List<Integer>> res = new ArrayList<List<Integer>>(); public List<List<Integer>> subsets(int[] nums) { List<Integer> temp = new ArrayList<>(); back(temp,nums, 0); return res; } voidback(List<Integer> temp,int[] nums, int prev){ List<Integer> list = new ArrayList<>(temp); //if(!res.contains(list)) res.add(list); for(int i = prev; i<nums.length; i++){ temp.add(nums[i]); back(temp, nums, i+1); temp.remove(temp.size()-1); } } }
// 90 subset2
sorting the array first and check the contains
class Solution { List<List<Integer>> res = new ArrayList<List<Integer>>(); public List<List<Integer>> subsetsWithDup(int[] nums) { List<Integer> temp = new ArrayList<>(); Arrays.sort(nums); back(temp,nums,0); return res; } void back(List<Integer> temp,int[] nums, int prev){ List<Integer> list = new ArrayList<>(temp); if(!res.contains(list)) res.add(list); for(int i = prev; i<nums.length; i++){ temp.add(nums[i]); back(temp, nums, i+1); temp.remove(temp.size()-1); } } }
Leetcode 78. Subsets (backtracking) 90 subset