您的位置:首页 > 其它

[leetCode] Subsets II

2015-05-08 23:55 323 查看
Given a collection of integers that might contain duplicates, nums, 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 nums = 
[1,2,2]
, a solution
is:
[
[2],
[1],
[1,2,2],
[2,2],
[1,2],
[]
]

public class Solution {
List<List<Integer>> res = new ArrayList<List<Integer>>();

public List<List<Integer>> subsetsWithDup(int[] nums) {
Arrays.sort(nums);
List<Integer> item = new ArrayList<Integer>();
res.add(item);
sub(nums, 0, item);
return res;
}

private void sub(int[] nums, int index, List<Integer> input) {
for (int i = index; i < nums.length; i++) {
List<Integer> item = new ArrayList<Integer>(input);
item.add(nums[i]);
if (res.contains(item) == false) {
res.add(item);
sub(nums, i + 1, item);
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Array Backtracking