[Leetcode] permutations ii 全排列
Given a collection of numbers that might contain duplicates, return all possible unique permutations.
For example,
[1,1,2]have the following unique permutations:
[1,1,2],[1,2,1], and[2,1,1].
题意:当数列中有重复数字时,求其全排列。
思路:和permutation一样。以[1,1,2]为例,以第一个1为开始,得到[1,1,2],[1,2,1],。因为排列是将顺序的,所以我们以第二1开始时,若是不去重,则得到的和以第一个1开始时一样,所以,这时,我们要跳过那些和前面相同的数字。这时,我们应该先对数列进行排序,然后,在向中间变量存入数字的时候,若此次遍历中,前面的没有访问过,跳过即可。参看了Grandyang的博客。代码如下:
class Solution {
public:
vector<vector<int> > permuteUnique(vector<int> &num)
{
vector<vector<int>> res;
vector<int> tempValue;
vector<int> visited(num.size(),);
sort(num.begin(),num.end()); //对其进行排序
helper(num,,visited, tempValue,res);
return res;
} void helper(vector<int> &num,int level,vector<int> visited, vector<int> &tempValue,
vector<vector<int>> &res)
{
if(level==num.size())
res.push_back(tempValue);
else
{
for(int i=;i<num.size();++i) //这里i=0
{
if(i>&&num[i]==num[i-]&&visited[i-]==) //在下一个if内也行
continue;
if(visited[i]==)
{
visited[i]=;
tempValue.push_back(num[i]);
helper(num,level+,visited,tempValue,res);
tempValue.pop_back();
visited[i]=;
}
}
}
}
};
[Leetcode] permutations ii 全排列的更多相关文章
- [LeetCode] Permutations II 全排列之二
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- leetcode Permutations II 无重全排列
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4051169.html 题目链接:leetcode Permutations II 无重全排 ...
- LeetCode: Permutations II 解题报告
Permutations II Given a collection of numbers that might contain duplicates, return all possible uni ...
- [LeetCode] 47. Permutations II 全排列之二
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- [LeetCode] 47. Permutations II 全排列 II
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- LeetCode Permutations II (全排列)
题意: 给出n个元素(可能有重复的),请产生出所有的全排列. 思路: 同版本1的有点不同,这次有可能含有重复的元素,很容易就TLE,节省时间才是关键点. 如果将一个序列中两个相同的元素交换,这个序列是 ...
- [leetcode]Permutations II @ Python
原题地址:https://oj.leetcode.com/problems/permutations-ii/ 题意: Given a collection of numbers that might ...
- leetcode -- Permutations II TODO
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- [Leetcode] Permutations II
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
随机推荐
- Kotlin对象:仅一行代码就可创建安全的单例
作者:Antonio Leiva 时间:Jun 20, 2017 原文链接:https://antonioleiva.com/objects-kotlin/ Kotlin对象是Android开发人员不 ...
- jmeter基础之录制篇
一.前言 jmeter如今被越来越多人喜爱的一款测试工具,相比于loadrunner它体积特轻便.jmeter不仅用来做单接口测试,压测还能做性能,主要是一款开源的,可以写一个你需要的插件功能再添加里 ...
- 谜题 (Puzzle,ACM/ICPC World Finals 1993,UVa227)
题目描述:算法竞赛入门经典习题3-5 题目思路:模拟题 #include <stdio.h> #include <string.h> #define maxn 55 char ...
- typescript 学习记录
类型判断: typeJudge() { //typeof 用来判断变量类型 var s: string = 'egret'; var isString: boolean = typeof s === ...
- java常见的异常类型
Exception分为两类:非运行是异常和运行时异常. java编译器要求方法必须声明抛出可能发生的非运行时异常,但是并不要求必须声明抛出未被捕获的运行时异常.A:NullPointerExcepti ...
- win7 个人电脑 IIS7服务器(web服务器) 同一局域网下均可访问本机网页
建立web服务器: 1.控制面板-->程序-->打开或关闭windows功能-->internet信息服务全部打钩,确定即可. 访问网页: 1.C:\inetpub\wwwroot\ ...
- JavaScript初探系列之数组的基本操作
在程序语言中数组的重要性不言而喻,JavaScript中数组也是最常使用的对象之一,数组是值的有序集合,由于弱类型的原因,JavaScript中数组十分灵活.强大,不像是Java等强类型高级语言数组只 ...
- android平台蓝牙编程(转)
http://blog.csdn.net/pwei007/article/details/6015907 Android平台支持蓝牙网络协议栈,实现蓝牙设备之间数据的无线传输. 本文档描述了怎样利用a ...
- C语言特殊符号
-> ->在C语言中称为间接引用运算符,是二目运算符,优先级同成员运算符“.”.用法:p->a,其中p是指向一个结构体的指针,a是这个结构体类型的一个成员.表达式p->a引用了 ...
- MFC消息处理
1.MFC窗口如何与AfxWndProc建立联系. 当一个新的CWnd派生类创建时,在调用CWnd::CreateEx()过程中,MFC都会安装AfxCbtFilterHook().这个Hook将拦截 ...