给定一个数组,返回所有的非重复的可能。例如给定

If S = [1,2,2], a solution is:

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

其实这题类似的有CombinationSubsets。有兴趣的可以看看之前的解题方法。那大概是第70题左右,十天以前做的吧。记得当时用了回溯法。现在这个还是要用到回溯。只是加了判断有重复的子集怎么办。

在回溯的过称中,记录一种可能后就把最后一个pop掉,然后回溯上一个的时候判断是不是和之前判断过的相等了。用一个tmpVal存刚刚pop的数字,如果相等,那就++跳过它。

class Solution {
public:
void dfs91(vector<vector<int> > &ans, vector<int> &tmp, vector<int> S, int start)
{
int tmpVal;
for (int i = start; i < S.size(); ++i)
{
tmp.push_back(S[i]);
dfs91(ans, tmp, S, i+);
ans.push_back(tmp);
tmpVal = tmp[tmp.size()-];
tmp.pop_back();
while(i+ < S.size() && tmpVal == S[i+]) i++; // 跳过和之前pop掉的数字相等的数,因为已经考虑过了
}
}
vector<vector<int> > subsetsWithDup(vector<int> &S)
{
vector<int> empty;
vector<vector<int> > ans(,empty); // 空集总是答案之一
sort(S.begin(), S.end()); // 排好序,才符合非降组合
if (S.size()==) return ans;
dfs91(ans, empty, S, );
return ans;
}
};


												

leetcode[91] Subsets II的更多相关文章

  1. [Leetcode Week8]Subsets II

    Subsets II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/subsets-ii/description/ Description Given ...

  2. 【leetcode】Subsets II

    Subsets II Given a collection of integers that might contain duplicates, S, return all possible subs ...

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

    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 (the ...

  5. Java for LeetCode 090 Subsets II

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

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

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

  7. 深度优先搜索算法(DFS)以及leetCode的subsets II

    深度优先搜索算法(depth first search),是一个典型的图论算法.所遵循的搜索策略是尽可能“深”地去搜索一个图. 算法思想是: 对于新发现的顶点v,如果它有以点v为起点的未探测的边,则沿 ...

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

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

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

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

随机推荐

  1. NSIS:迅雷5.8.6.600自由定制版脚本及下载

    原文 NSIS:迅雷5.8.6.600自由定制版脚本及下载 虽然现在迅雷已升级到7.2版本,但作为一款下载工具,我们可能只是需要它的下载功能,所以,体积小资源占用也小的迅雷5便成了我们更好的选择.今天 ...

  2. redis和redis php扩展安装(转)

    redis是一个内存数据库,比memcache支持更丰富的value类型,新浪微博就使用redis来做缓存. redis的源码安装 wget http://download.redis.io/redi ...

  3. Android GPS获取当前经纬度坐标

    APP中可能会遇到一种需求,就是将当前所在位置的坐标传到server上,今天我提供三种途径去获取经纬度坐标信息,第一种是通过Android API来实现,另外一种通过百度地图API来实现,第三种通过天 ...

  4. ssh无密码登陆(转)

    [0]写在前面 由于ssh 实现的是免密码登陆,大致步骤是: 0.1) client通过ssh登陆到server: 0.2) server检查家目录下的.ssh文件, 并发送公钥文件 authoriz ...

  5. Writing your first Django app, part 1(转)

    Let’s learn by example. Throughout this tutorial, we’ll walk you through the creation of a basic pol ...

  6. Git与TortoiseGit基本操作(转)

    1. GitHub操作 本节先简单介绍 git 的使用与操作, 然后再介绍 TortoiseGit 的使用与操作. 先看看SVN的操作吧, 最常见的是 检出(Check out ...), 更新 (U ...

  7. Redis源代码分析(十一年)--- memtest内存测试

    今天,我们继续redis源代码test下测试在封装中的其它文件.今天读数memtest档,翻译了,那是,memory test 存储器测试工具..可是里面的提及了非常多东西,也给我涨了非常多见识,网上 ...

  8. hdu 4542 数论 + 约数个数相关 腾讯编程马拉松复赛

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=4542 小明系列故事--未知剩余系 Time Limit: 500/200 MS (Java/Others) ...

  9. MAC OSX在视图port哪个程序占用,杀死进程的方法

    sudo lsof -i :9000 COMMAND   PID    USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME java    6 ...

  10. 华为OJ: 公共字符串计算

    有几个需要注意的地方,这个问题是不是大写和小写之间的区别.这样你就输入字符串大写或小写转换的计算前. 第二个,定要清晰.先将s1从[i]处開始与s2的[j]開始匹配,不相等则j++直到j等于s2.le ...