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

subsets这道题非常相像,除了S中会有重复的元素,那么会有什么影响呢?看一个例子:

S = [1,1]

根据subsets中的算法,生成的子集如下:

[
[],
[1],
[1],
[1,1]
]

显然[1],[1]这两个是重复的,需要去掉。那么我们怎么判重?只需要在递归函数的循环里面加上如下判断

if(i != pos && s[i] == s[i - 1]) continue; 就可以了。

理解这个判断的关键,是搞清楚递归函数

public void subsetsDfs(int[] s,int level,int pos,ArrayList<Integer> result,List<List<Integer>> answer)

中pos变量的含义。

在这个函数中,level表示要生成的子集的长度,pos则表示目前要生成子集的第pos位上的元素。那么当i==pos的时候,说明子集中第pos位上的元素还没有生成过,那么即使s[i]==s[i-1],它们在生成的子集中所占有的位置不同(例如例子中的[1,1]中的两个1分别在生成的子集的第0位和第1位上),s[i]就可以加到这个子集上去。但是当i != pos的时候,说明生成的子集上第pos位已经有元素了,如果当前的s[i]和这个元素s[i-1]相等,那么就没有必要再在这个位置上重复放置s[i]了(例如上述例子中已经有子集[1]的时候,不能够再生成子集[s[1]=1]了)。

最后实现的代码如下:

 public class Solution {
public void subsetsDfs(int[] s,int level,int pos,ArrayList<Integer> result,List<List<Integer>> answer){
if(result.size() == level){
answer.add(new ArrayList<Integer>(result));
return;
}
for(int i = pos;i < s.length;i++){
if(i != pos && s[i] == s[i - 1])
continue;
result.add(s[i]);
subsetsDfs(s, level, i+1, result, answer);
result.remove(result.size()-1);
}
}
public List<List<Integer>> subsetsWithDup(int[] S) {
Arrays.sort(S);
List<List<Integer>> answer = new ArrayList<List<Integer>>();
ArrayList<Integer> result = new ArrayList<Integer>(); for(int i = 0;i <= S.length;i++)
subsetsDfs(S, i, 0, result, answer);
return answer;
}
}

【leetcode刷题笔记】Subsets II的更多相关文章

  1. LeetCode刷题笔记和想法(C++)

    主要用于记录在LeetCode刷题的过程中学习到的一些思想和自己的想法,希望通过leetcode提升自己的编程素养 :p 高效leetcode刷题小诀窍(这只是目前对我自己而言的小方法,之后会根据自己 ...

  2. LeetCode刷题笔记 - 12. 整数转罗马数字

    学好算法很重要,然后要学好算法,大量的练习是必不可少的,LeetCode是我经常去的一个刷题网站,上面的题目非常详细,各个标签的题目都有,可以整体练习,本公众号后续会带大家做一做上面的算法题. 官方链 ...

  3. 18.9.10 LeetCode刷题笔记

    本人算法还是比较菜的,因此大部分在刷基础题,高手勿喷 选择Python进行刷题,因为坑少,所以不太想用CPP: 1.买股票的最佳时期2 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. ...

  4. Leetcode刷题笔记(双指针)

    1.何为双指针 双指针主要用来遍历数组,两个指针指向不同的元素,从而协同完成任务.我们也可以类比这个概念,推广到多个数组的多个指针. 若两个指针指向同一数组,遍历方向相同且不会相交,可以称之为滑动窗口 ...

  5. 【leetcode刷题笔记】Word Ladder II

    Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...

  6. 【leetcode刷题笔记】Palindrome Partitioning II

    Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...

  7. 【leetcode刷题笔记】Subsets

    Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be ...

  8. 【leetcode刷题笔记】Best Time to Buy and Sell Stock II

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  9. 【leetcode刷题笔记】Populating Next Right Pointers in Each Node II

    What if the given tree could be any binary tree? Would your previous solution still work? Note: You ...

随机推荐

  1. SELECT * INTO xx FROM x0

    insert into a select * from b:--向存在表中插入数据,如果不存在表a报错. select * into a from b:--创建新表的同时插入数据,如果表a存在,报错. ...

  2. Snail—UI学习之UITextField

    简单看一下UITextField的属性 - (void)createTextField{ UITextField * textField = [[UITextField alloc] initWith ...

  3. linux 下gtest 安装

    cd gtest_dir //解压后的目录 mkdir mybuild # Create a directory to hold the build output. cd mybuild cmake ...

  4. JS input 银行卡号格式转换

    replace(/\D/g,'').replace(/....(?!$)/g,'$& ')

  5. sklearn函数白板

    #使用make_classification构造500个样本,每个样本有20个feature from sklearn.datasets import make_classification X, y ...

  6. jeesite中activiti中的流程表梳理

    最近在利用jeesite开发一个小系统,趁着这个机会整理了activiti中的相关表,跟踪流程,然后查看这几个表中数据的变化,可以更好地理解流程的开发.现在整理出来,希望可以帮助更多的人! 表结构 一 ...

  7. AjaxPro.2.dll AjaxPro.AjaxMethod 前后台交互

    我们需要下载 AjaxPro.2.zip.然后把下载到的 AjaxPro.2.dll 的文件引入到项目. 1.接着,在 Web.config 的 <system.web> 标签下写入以下内 ...

  8. 用代码构造PreferenceScreen

    在PreferenceFregment中构造界面,简单省事的方法就是使用findPreference然后在xml里把UI写好.在代码中动态的添加UI内容也是需要的.核心代码是: PreferenceS ...

  9. 图像处理之基础---傅立叶c实现

    http://blog.csdn.net/lzhq28/article/details/7847047 http://blog.csdn.net/lishizelibin/article/detail ...

  10. python 基础 9.11 更改数据

    #/usr/bin/python #-*- coding:utf-8 -*- #@Time   :2017/11/24 4:45 #@Auther :liuzhenchuan #@File   :更改 ...