一天一道LeetCode

本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github

欢迎大家关注我的新浪微博,我的新浪微博

欢迎转载,转载请注明出处

(一)题目

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

 []

]

(二)解题

题目大意:求给定数集中所有子集,注意数集中有重复的数字。

可以参考:【一天一道LeetCode】#78. Subsets,但是按照78题中的解法再最后去重的话就显得有些麻烦,于是采用回溯法在过程中去重。

  1. class Solution {
  2. public:
  3. vector<vector<int>> subsetsWithDup(vector<int>& nums) {
  4. vector<int> temp;
  5. vector<vector<int>> ret;
  6. int last;
  7. sort(nums.begin(),nums.end());//首先需要排序,便于在过程中去重
  8. dfsSubset(ret,nums,temp,0,last);
  9. return ret;
  10. }
  11. void dfsSubset(vector<vector<int>>&ret , vector<int>& nums , vector<int>& temp , int i ,int& last)
  12. {
  13. ret.push_back(temp);
  14. for(int j = i ; j < nums.size() ; j++)
  15. {
  16. if(last == nums[j]) continue;//记录上一次push进去的数,如果相同,就代表重复了,需要去除
  17. temp.push_back(nums[j]);
  18. dfsSubset(ret,nums,temp,j+1,last);
  19. last = nums[j];//每次记录push进去的数
  20. temp.pop_back();//回溯到上一次的结果
  21. }
  22. }
  23. };

【一天一道LeetCode】#90. Subsets II的更多相关文章

  1. [LeetCode] 90.Subsets II tag: backtracking

    Given a collection of integers that might contain duplicates, nums, return all possible subsets (the ...

  2. [leetcode]90. Subsets II数组子集(有重)

    Given a collection of integers that might contain duplicates, nums, return all possible subsets (the ...

  3. [LeetCode] 90. Subsets II 子集合 II

    Given a collection of integers that might contain duplicates, nums, return all possible subsets (the ...

  4. LeetCode 90. Subsets II (子集合之二)

    Given a collection of integers that might contain duplicates, nums, return all possible subsets. Not ...

  5. leetCode 90.Subsets II(子集II) 解题思路和方法

    Given a collection of integers that might contain duplicates, nums, return all possible subsets. Not ...

  6. [LeetCode] 90. Subsets II 子集合之二

    Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: ...

  7. Leetcode#90 Subsets II

    原题地址 跟Subsets(参见这篇文章)类似. 但因为有重复元素,所以要考虑去重问题. 什么情况下会出现重复呢?比如S = {5, 5, 5},如果要选1个5,一共有C(3,1)=3种选法,即100 ...

  8. leetcode 78. Subsets 、90. Subsets II

    第一题是输入数组的数值不相同,第二题是输入数组的数值有相同的值,第二题在第一题的基础上需要过滤掉那些相同的数值. level代表的是需要进行选择的数值的位置. 78. Subsets 错误解法: cl ...

  9. LeetCode Problem 90. Subsets II

    python solution 123456789101112131415161718192021222324252627 class (object): def subsetsWithDup(sel ...

随机推荐

  1. Intellij Error:Cannot build Artifact 'XXX:war exploded' because it is included into a circular dependency

    外网的流程是这样的. 1: 2: 3: 4: 基本按这个来就好了 如果到了build artfact哪里按钮是灰色 就要手动建了 https://jingyan.baidu.com/album/0a5 ...

  2. sololearn的c++学习记录_4m11d

    Function Templates Functions and classes help to make programs easier to write, safer, and more main ...

  3. 独立游戏《Purgatory Ashes》的经验与总结

    1.引子 游戏的灵感萌生于2015年,当时只有一些概念性的设计图. 后来我利用资源商店的素材搭建了最早的原型. 游戏的最终画面: 早期以D.P作为代号进行开发,来源于两个单词的缩写 Devil Pro ...

  4. 利用JAVA多线程来提高数据处理效率

    肿瘤大数据挖掘中经常需要处理上百亿行的文本文件,这些文件往往高达数百GB,假如文件结构简单统一,那么用sed和awk 处理是非常方便和快速的.但有时候会遇到逻辑较为复杂的处理流程,这样我一般会用JAV ...

  5. POJ 3050 Hopscotch DFS

    The cows play the child's game of hopscotch in a non-traditional way. Instead of a linear set of num ...

  6. jQuery 捕获

    jQuery 拥有可操作 HTML 元素和属性的强大方法. jQuery DOM 操作 jQuery 中非常重要的部分,就是操作 DOM 的能力. jQuery 提供一系列与 DOM 相关的方法,这使 ...

  7. Docker Kubernetes 项目

    Kubernetes 是 Google 团队发起并维护的基于Docker的开源容器集群管理系统,它不仅支持常见的云平台,而且支持内部数据中心. 建于Docker之上的Kubernetes可以构建一个容 ...

  8. webpack dev server 和 sublime text 配合时需要注意的地方

    参考:https://webpack.js.org/guides/development/ Adjusting Your Text Editor Some text editors have a &q ...

  9. iOS图形手势识别框架SGGestureRecognizer

    简介 苹果官方为我们提供了简单手势的识别器,但对于图形手势,例如五角星.三角形等的识别,就需要自己实现了.通过识别这些手势,可以去执行特定的操作,或是输入公式.释放魔法等,可以为App增光添彩. 下载 ...

  10. Ubuntu环境下Anaconda安装TensorFlow并配置Jupyter远程访问

    本文主要讲解在Ubuntu系统中,如何在Anaconda下安装TensorFlow以及配置Jupyter Notebook远程访问的过程. 在官方文档中提到,TensorFlow的安装主要有以下五种形 ...