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: ...
随机推荐
- js控制文本框仅仅能输入中文、英文、数字与指定特殊符号
JS 控制文本框仅仅能输入数字 <input onkeyup="value=value.replace(/[^0-9]/g,'')"onpaste="value=v ...
- NSIS:静默释放文件并运行 制作绿色单文件软件
原文 NSIS:静默释放文件并运行 制作绿色单文件软件 现在所谓的绿色单文件软件,大多与以下代码原理相似:把软件运行需要的文件封装为一个EXE文件,双击时释放到某个目录(大多是TEMP)并运行主程序文 ...
- 单机部署redis主从备份
redis为了避免单点故障,也支持主从备份.个人在做主从备份的实验时,因为机器数量有限,一般非常少有多台机器做支撑. 本文就将叙述怎样在一台电脑上实现redis的主从备份. 同一台机器上部署多个red ...
- Ruby 一些经常使用的细节
1.try 永远不会抛出异常 在 没有的时候 返回 nil province_id = Province.find_by_name(prov).try(:id) 2.find(:first, :con ...
- C++它 typedef void *HANDLE
阅读时编写代码的代码,经常看到一个代码: typedef void *HANDLE ,这是它背后的故事?怎么理解呢? 不明白的时候.这是非常美妙的感觉,后来我才知道这,这是typedef定义,就在vo ...
- eclipse环境下如何配置tomcat
eclipse环境下如何配置tomcat 很多初学,尤其自学JavaWeb的朋友首次在eclipse下配置tomcat时,总会有种难下手的感觉,在此,通过图文解说的方法,最直观的向大家演示一遍该配置过 ...
- JavaEE(12) - JPA规范及实现(TopLink和Hibernate)
1. JPA规范与ORM框架之间的联系 JPA规范并不属于EJB3规范,它是一套完全独立的规范,不仅可以在基于EJB的JavaEE应用程序中使用,而且完全可以在普通JavaSE应用程序中使用. JPA ...
- UVa 11790 - Murcia's Skyline
称号:给你一个行长度的建设和高度,我们祈求最长的和下降的高度. 分析:dp,最大上升子. 说明:具有长度,不能直接优化队列单调. #include <iostream> #include ...
- 第20章 状态模式(State Pattern)
原文 第20章 状态模式(State Pattern) 状态模式 概述: 当一个对象的内在状态改变时允许改变其行为,这个对象看起来像是改变了其类. 状态模式主要解决的是当控制一个对象状态的条件表 ...
- hdu 1098 Ignatius's puzz
有关数论方面的题要仔细阅读,分析公式. Problem Description Ignatius is poor at math,he falls across a puzzle problem,so ...