leetcode[91] Subsets II
给定一个数组,返回所有的非重复的可能。例如给定
If S = [1,2,2]
, a solution is:
[ [2], [1], [1,2,2], [2,2], [1,2], [] ]
其实这题类似的有Combination和Subsets。有兴趣的可以看看之前的解题方法。那大概是第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的更多相关文章
- [Leetcode Week8]Subsets II
Subsets II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/subsets-ii/description/ Description Given ...
- 【leetcode】Subsets II
Subsets II Given a collection of integers that might contain duplicates, S, return all possible subs ...
- [LeetCode] 90.Subsets II tag: backtracking
Given a collection of integers that might contain duplicates, nums, return all possible subsets (the ...
- [leetcode]90. Subsets II数组子集(有重)
Given a collection of integers that might contain duplicates, nums, return all possible subsets (the ...
- Java for LeetCode 090 Subsets II
Given a collection of integers that might contain duplicates, nums, return all possible subsets. Not ...
- [LeetCode] 90. Subsets II 子集合 II
Given a collection of integers that might contain duplicates, nums, return all possible subsets (the ...
- 深度优先搜索算法(DFS)以及leetCode的subsets II
深度优先搜索算法(depth first search),是一个典型的图论算法.所遵循的搜索策略是尽可能“深”地去搜索一个图. 算法思想是: 对于新发现的顶点v,如果它有以点v为起点的未探测的边,则沿 ...
- LeetCode 90. Subsets II (子集合之二)
Given a collection of integers that might contain duplicates, nums, return all possible subsets. Not ...
- [LeetCode] 90. Subsets II 子集合之二
Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: ...
随机推荐
- Android4.0(Phone)来电过程分析
在开机时.系统会启动PhoneApp类,那是由于在AndroidManifest.xml文件里配置了 <application android:name="PhoneApp" ...
- BZOJ 1015 JSOI2008 星球大战 starwar 并检查集合
标题效果:给定一个无向图.联通谋求块的数目,以及k一个点的破坏后每次:联通,块的数目 侧面和摧毁的地步全记录,我们可以做相反的. 需要注意的是该点不能算作破坏联通块 #include<cstdi ...
- MYSQL-用户权限的验证过程(转)
知识点 因为MySQL是使用User和Host两个字段来确定用户身份的,这样就带来一个问题,就是一个客户端到底属于哪个host. 如果一个客户端同时匹配几个Host,对用户的确定将按照下面的优先级来排 ...
- 浅谈Hybrid技术的设计与实现(转)
前言 随着移动浪潮的兴起,各种APP层出不穷,极速的业务扩展提升了团队对开发效率的要求,这个时候使用IOS&Andriod开发一个APP似乎成本有点过高了,而H5的低成本.高效率.跨平台等特性 ...
- zoj 3203 Light Bulb,三分之二的基本问题
Light Bulb Time Limit: 1 Second Memory Limit: 32768 KB Compared to wildleopard's wealthiness, h ...
- Git 命令速查表
Git 命令速查表 1.常用的Git命令 命令 简要说明 git add 添加至暂存区 git add-interactive 交互式添加 git apply 应用补丁 git am 应用邮件格式补丁 ...
- hdu 最大三角形(凸包+旋转卡壳)
老师在计算几何这门课上给Eddy布置了一道题目,题目是这样的:给定二维的平面上n个不同的点,要求在这些点里寻找三个点,使他们构成的三角形拥有的面积最大.Eddy对这道题目百思不得其解,想不通用什么方法 ...
- DynamicReports 导出Excel 例子
import java.awt.Color; import java.io.FileOutputStream; import java.util.ArrayList; import java.util ...
- Windows移动开发(一)——登堂入室
開始本博客之前先分享一个自己的好消息吧,2014年3月31日起,正式就职于北京****集团Win8project师.主要负责将IOS和Android应用移植到Win8.1平板上,目标客户是银行,闲话不 ...
- Caused by: java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory
1.错误叙述性说明 2014-7-12 0:38:57 org.apache.catalina.core.ApplicationContext log 信息: No Spring WebApplica ...