[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 ...
随机推荐
- “网易有钱”sketch使用分享
本文来自网易云社区 写在开头,关于ps与sketch之间的优劣网上已经有很多分享,大家有兴趣可以百度,其中对否我们在这里不予评价.在移动互联网时代每个app从几十到上百张页面,如果用ps绘制一个个页面 ...
- 两分钟了解Docker的优势
本文来自网易云社区 我们主要从Docker对业务架构和生产实践的角度来分析. 随着业务规模的逐渐扩大,产品复杂度也随着增加,企业需要解决快速迭代.高可靠和高可用等问题,一个自然的选择是服务化的拆分,把 ...
- OpenSUSE 11 安装Qt5.0,失败,失败,失败,留个坑,以后来填,万一实现了呢
我又来无耻的写问题来了,这次还真的是没有解决,线留坑吧,万一以后实现了. 同样,这次也是以恶搞网友说听说想在open suse 上面安装5.0版本以后的Qt,自己折腾好几没有成功. 我一想,哎,这不是 ...
- Visual Studio Code 工具使用教程
软件下载这里就不用讲了,安装完之后: 1.切换中文: 选择扩展 搜索“Language”,在下列选项选择 Chinese (Simplified) Language Pack for Visual S ...
- 第三模块:面向对象&网络编程基础 第3章 选课系统作业讲解
01-选课系统作业讲解1 02--选课系统作业讲解2 03-选课系统作业讲解3 04--选课系统作业讲解4 01-选课系统作业讲解1 02--选课系统作业讲解2 03-选课系统作业讲解3 04--选课 ...
- 使用jenkins构建一个自由风格的项目
一.创建一个freestyle风格的构建项目 二.输入任务名称和选择任务类型 三.配置项目 3.1:选择代码托管 3.2:到gitlab上去配置deploy key 3.3:到jenkins服务器去生 ...
- Python2快速入门教程,只需要这十五张图片就够了!
今天给大家分享的教程是适用于Python 2.7,但它可能适用于Python 2.Python 2.7将停止在2020中的支持. 与Python 2.7和3兼容的Python代码是完全可能的.通过使用 ...
- 如何区别cookie和token?---测试cookie和token接口时先看。
cookie 是什么? cookie--------------在浏览器中的长相?火狐浏览器 ----------------------------------------------------- ...
- 【Python 开发】第三篇:python 实用小工具
一.快速启动一个web下载服务器 官方文档:https://docs.python.org/2/library/simplehttpserver.html 1)web服务器:使用SimpleHTTPS ...
- 改maven下创建的动态网站依赖的jre版本
问题描述 通过maven创建一个动态网站后,eclipse会提示一个提醒 Build path specifies execution environment J2SE-1.5. There are ...