Leetcode90. Subsets II
关键词:backtracking
1 class Solution { 2 public List<List<Integer>> subsetsWithDup(int[] nums) { 3 List<List<Integer>> ans = new ArrayList<>(); 4 Arrays.sort(nums); 5 helper(ans,new ArrayList<>(),nums,0); 6 return ans; 7 8 } 9 private void helper(List<List<Integer>> ans,List<Integer> tempList,int[] nums,int start){ 10 ans.add(new ArrayList<>(tempList)); 11 for(int i=start;i<nums.length;i++){ 12 if(i>start&&nums[i]==nums[i-1]) continue; //key point 13 tempList.add(nums[i]); 14 helper(ans,tempList,nums,i+1); 15 tempList.remove(tempList.size()-1); 16 } 17 } 18 }
3ms 48.56%可以了。
参考39题discuss。

浙公网安备 33010602011771号