标题: | |
通过率: | 27.5 |
难度: | 中等 |
Given a collection of integers that might contain duplicates, S, return all possible subsets.
Note:
- Elements in a subset must be in non-descending order.
- The solution set must not contain duplicate subsets.
For example,
If S =[1,2,2]
, a solution is: [ [2], [1], [1,2,2], [2,2], [1,2], []]
从第一个版本中可以看出来,这里需要增加相同判断。具体看代码
1 public class Solution { 2 public ArrayList> subsetsWithDup(int[] num) { 3 ArrayList > res=new ArrayList >(); 4 ArrayList tmp=new ArrayList (); 5 Arrays.sort(num); 6 dfs(res,tmp,0,num); 7 return res; 8 } 9 public void dfs(ArrayList > res,ArrayList tmp,int start,int[] num){10 if(!res.contains(tmp))11 res.add(new ArrayList (tmp));12 for(int i=start;i