首页 > 其他 > 详细

Subsets II

时间:2015-07-17 02:14:13      阅读:251      评论:0      收藏:0      [点我收藏+]

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 {
	public List<List<Integer>> subsetsWithDup(int[] nums) {
    	List<List<Integer>> res = new ArrayList<List<Integer>>();
    	if (nums.length<=0 || nums==null) {
    		return res;
    	}
    	Arrays.sort(nums);
    	ArrayList<Integer> list = new ArrayList<Integer>();
    	for (int i = 1; i <= nums.length; i++) {
    		dfs(nums, 0, i, list, res);
    	}
    	res.add(new ArrayList<Integer>());
    	return res;
    }

	private void dfs(int[] arr, int start, int len, ArrayList<Integer> list,
			List<List<Integer>> res) {
		if (list.size() == len) {
			if (!res.contains(list)) {
				res.add(new ArrayList<Integer>(list));
			}
			return;
		}
		for (int i = start; i < arr.length; i++) {
			list.add(arr[i]);
			dfs(arr, i+1, len, list, res);
			list.remove(list.size()-1);
		}
	}
}
?

Subsets II

原文:http://hcx2013.iteye.com/blog/2227981

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