首页 > 代码库 > leetcode : subsetsII
leetcode : subsetsII
Given a collection of integers that might contain duplicates, nums, return all possible subsets.
Note: 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>> result = new ArrayList<List<Integer>>(); if(nums == null || nums.length == 0) { return result; } List<Integer> list = new ArrayList<Integer>(); Arrays.sort(nums); backtrack(result, list, nums, 0); return result; } public void backtrack(List<List<Integer>> result, List<Integer> list, int[] nums, int position) { if(list.size() > nums.length) { return; } result.add(new ArrayList<Integer>(list)); for(int i = position; i < nums.length; i++) { if(i > position && nums[i] == nums[i - 1]) { continue; } list.add(nums[i]); backtrack(result, list, nums, i + 1); list.remove(list.size() - 1); } } }
leetcode : subsetsII
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。