首页 > 代码库 > [LeetCode]Subsets II
[LeetCode]Subsets II
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], [] ]递归+增量构造法
public class Solution { List<List<Integer>> res = new ArrayList<List<Integer>>(); List<Integer> list = new ArrayList<Integer>(); public List<List<Integer>> subsetsWithDup(int[] num) { Arrays.sort(num); help(num,0); return res; } private void help(int[]num,int start){ if(!res.contains(list)){ List<Integer> lin = new ArrayList<Integer>(list); res.add(lin); } for(int i=start;i<num.length;i++){ list.add(num[i]); help(num,i+1); list.remove(Integer.valueOf((num[i]))); } } }
[LeetCode]Subsets II
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。