首页 > 其他 > 详细

[Leetcode] Subsets II

时间:2014-11-13 09:21:26      阅读:238      评论:0      收藏:0      [点我收藏+]

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],
  []
]

Solution:

 1 public class Solution {
 2     public List<List<Integer>> subsetsWithDup(int[] num) {
 3         Arrays.sort(num);
 4         List<List<Integer>> result = new ArrayList<List<Integer>>();
 5         List<Integer> al = new ArrayList<Integer>();
 6         dfs(result, al, num, 0);
 7         return result;
 8     }
 9 
10     private void dfs(List<List<Integer>> result, List<Integer> al, int[] s,
11             int start) {
12         // TODO Auto-generated method stub
13         result.add(new ArrayList<Integer>(al));
14         for (int i = start; i < s.length; ++i) {
15             al.add(s[i]);
16             dfs(result, al, s, i+1);
17             al.remove(al.size()-1);
18             while((i+1<s.length)&&(s[i]==s[i+1]))
19                 ++i;
20         }
21     }
22 }

 

[Leetcode] Subsets II

原文:http://www.cnblogs.com/Phoebe815/p/4094064.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!