首页 > 代码库 > 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], []]
每一次都在前一次的基础上添加新的数就行了。
1 public class Solution { 2 public List<List<Integer>> subsetsWithDup(int[] num) { 3 List<List<Integer>> result = new ArrayList<List<Integer>>(); 4 result.add(new ArrayList<Integer>()); 5 if (num.length == 0) { 6 return result; 7 } 8 Arrays.sort(num); 9 for (int i = 0; i < num.length; i++) {10 List<List<Integer>> current = new ArrayList<List<Integer>>(result);11 for (List<Integer> list : result) {12 List<Integer> newlist = new ArrayList<Integer>(list);13 newlist.add(num[i]);14 if (!current.contains(newlist)) {15 current.add(newlist);16 }17 }18 result = current;19 }20 return result;21 }22 }
LeetCode Subsets II
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。