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: ...
随机推荐
- SQLServer 2012异常问题(二)--由安装介质引发性能问题
原文:SQLServer 2012异常问题(二)--由安装介质引发性能问题 问题描述:生产环境一个数据库从SQLSERVER 2008 R2升级到SQLSERVER 2012 ,同时更换硬件,但迁移后 ...
- Unity3D脚本--真实1
1. Unity3D动作脚本 Unity3D脚本用于Unity3D发动机订单公布. JavaScript全局变量:在Inspector中能够看到,且能够改动其值.其他脚本可调用此变量. C#公有(pu ...
- Android内置下拉刷新组件SwipeRefreshLayout
也许下拉刷新之前,你可能会使用一些第三方的开源库,例如PullToRefresh, ActionBar-PullToRefresh等待,但现在有的正式组成部分---SwipeRefreshLayout ...
- oracle当需要commit
今天oracle的SQL plus 运行该删除和查询操作,然后PL/SQL还运行查询.相同的句子,结果是不一样的.我郁闷很大,然后,突然想到这可能对双方造成由数据不一致,为什么不一致呢.就是没用com ...
- 网络资源(1) - Hadoop视频
2014_08_23: hadoop03c_分布式文件系统HDFS http://v.youku.com/v_show/id_XNDgwNjg1OTY0.html?f=18604686 2014_08 ...
- Android第一次打开应用程序,实现向导界面
转载请注明出处,谢谢http://blog.csdn.net/harryweasley/article/details/42079167 先说下思路:1.利用Preference存储数据,来记录是否是 ...
- windows下系统移植到linux下出现的问题
今天遇到了一个之前没有遇到的问题,记录一下. 我们是在windows下进行开发的,最终系统是部署在linux服务器上. 在windows一切正常,但是部署到linux下时,有些功能不能用了.通过log ...
- ssh 综合
文件夹结构: 搭建项目: 1.创建web项目 2.创建各种包. com.atguigu.surveypark.dao.impl com.atguigu.surveypark.model com.atg ...
- linux_java_redis_postgresql_常用命令
redis 常用语法telnet 192.168.18.210 6379keys *llen队列名称llen 队列名称 postgresql常用语法psql -h192.168.18.210 -Up ...
- crawler_分布式网络爬虫的设计与实现_设计图
一.集中调度式 二.p2p 三.混合调度式 四.大型集群