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. 那些奇妙的&quot;大师&quot;是怎样炼成的(科学、迷信、心理)

    近期王林大师从神坛上掉下来直接掉进了监狱,有关他的非常多神话也相同被撕下了. 事实上这类奇妙的大师在地球上非常多,美国的非常多"邪教"头目,国内的邪教头目都属于这一类.国内比較轰动 ...

  2. win7不休眠方式设置

    方式1:命令行下以管理员方式执行:powercfg -h off 方式2:右键个性化-->屏幕保护程序-->更改电源设置--->更改计算机睡眠时间--->是计算机进入睡眠状态选 ...

  3. Spring使用经验之StandardServletMultipartResolver实现文件上传的基本配置

    Note:Spring使用版本是4.1.6.RELEASE 1. 在实现了AbstractAnnotationConfigDispatcherServletInitializer的类中重载custom ...

  4. Jenkins与Docker相关的Plugin使用

    原文地址:http://blog.csdn.net/ztsinghua/article/details/52128140 Jenkins与Docker相关的Plugin 在Jenkins Plugin ...

  5. webAPI 405

    web.config 配置 <system.webServer> <modules> <remove name="WebDAVModule" /> ...

  6. Apollo配置中心解惑(一):关于一个portal管理多个环境,要求环境相互之间不影响,独立

    关于作者的回答很官方,不太懂: https://github.com/ctripcorp/apollo/wiki/%E5%88%86%E5%B8%83%E5%BC%8F%E9%83%A8%E7%BD% ...

  7. synchronized加static区别

    在多线程中,在synchronise方法上加上static 表示的是类锁,锁住的是整个类.而synchroinized 锁住的是当前方法,当前对象.

  8. quartz项目中的运用

    下面是之前项目中quartz的运用,我将它梳理出来. 测试类: public class OrdExpireTaskMain { public static void main(String[] ar ...

  9. java中双向链表的增、删、查操作

    import java.util.NoSuchElementException; public class DoublyLinkedListImpl<E> { private Node h ...

  10. [译]GLUT教程 - 弹出菜单基础

    Lighthouse3d.com >> GLUT Tutorial >> Pop-up Menus >> Popup Menus 弹出菜单也是GLUT的一部分.虽然 ...