public List<List<Integer>> subsetsWithDup(int[] num) {
List<List<Integer>> result = new ArrayList<List<Integer>>();
result.add(new ArrayList<Integer>());
Arrays.sort(num);
int startIndex = 0;
for (int i=0; i<num.length; i++) {
int size = result.size();
for (int j = startIndex; j < size; j++) {
List<Integer> sub = new ArrayList<Integer>();
sub.addAll(result.get(j));
sub.add(num[i]);
result.add(sub);
}
if (i<num.length-1 && num[i+1] == num[i]) {
startIndex = size;
} else {
startIndex = 0;
}
}
return result;
}
No comments:
Post a Comment