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].
class Solution {
private:
vector<vector<int> > ret;
public:
void perm(vector<int> &num,int i,int len){
if(i==len){
ret.push_back(num);
return;
}
for(int j=i;j<len;++j){
if(!isSwap(num,i,j))
continue;
swap(num[i],num[j]);
perm(num,i+,len);
swap(num[j],num[i]);
}
}
bool isSwap(vector<int>& num, int i, int j) {
while (num[i] != num[j] && i < j) i++;
if (i == j) return true;
else return false;
}
vector<vector<int> > permuteUnique(vector<int> &num) {
int len=num.size();
ret.clear();
sort(num.begin(), num.end());
perm(num,,len);
return ret;
}
};
参考http://blog.csdn.net/morewindows/article/details/7370155
Permutations II 去掉重复的全排列的更多相关文章
- 047 Permutations II 有重复数字的全排列
给定一个可能包含重复数字的集合,返回所有可能的不同全排列.例如,[1,1,2] 有以下不同全排列:[ [1,1,2], [1,2,1], [2,1,1]] 详见:https://leetcode ...
- leetcode Permutations II 无重全排列
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4051169.html 题目链接:leetcode Permutations II 无重全排 ...
- Leetcode之回溯法专题-47. 全排列 II(Permutations II)
Leetcode之回溯法专题-47. 全排列 II(Permutations II) 给定一个可包含重复数字的序列,返回所有不重复的全排列. 示例: 输入: [1,1,2] 输出: [ [1,1,2] ...
- [Swift]LeetCode47. 全排列 II | Permutations II
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- LeetCode 47. 全排列 II(Permutations II)
题目描述 给定一个可包含重复数字的序列,返回所有不重复的全排列. 示例: 输入: [1,1,2] 输出: [ [1,1,2], [1,2,1], [2,1,1] ] 解题思路 类似于LeetCode4 ...
- Leetcode47. Permutations II全排列2
给定一个可包含重复数字的序列,返回所有不重复的全排列. 示例: 输入: [1,1,2] 输出: [ [1,1,2], [1,2,1], [2,1,1] ] 在全排列1题目的基础上先排序,目的是把相同的 ...
- LeetCode:Permutations, Permutations II(求全排列)
Permutations Given a collection of numbers, return all possible permutations. For example, [1,2,3] h ...
- Permutations II - LeetCode
目录 题目链接 注意点 解法 小结 题目链接 Permutations II - LeetCode 注意点 不确定有几种排列 解法 解法一:因为有重复的数字所以排列的个数不确定几个,一直生成新的排列直 ...
- leetcode47. Permutations II
leetcode47. Permutations II 题意: 给定可能包含重复的数字的集合,返回所有可能的唯一排列. 思路: 这题全排列两种写法. 用hash表记录已经visit的数字,虽然看起来很 ...
随机推荐
- day72作业
目录 models模型类 路由配置 视图配置 序列化组件配置 基于ModelSerializer类,完成Car资源的单查,群查,单增接口 序列化:显示车名,车的颜色,车的价格,车的海报,车的品牌 反序 ...
- 微信audio自动播放(ios播放问题)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- (转载)JavaScript世界万物诞生记
一. 无中生有 起初,什么都没有.造物主说:没有东西本身也是一种东西啊,于是就有了null: 现在我们要造点儿东西出来.但是没有原料怎么办?有一个声音说:不是有null嘛?另一个声音说:可是null代 ...
- 使用edac工具来检测服务器内存故障.
随着虚拟化,Redis,BDB内存数据库等应用的普及,现在越来越多的服务器配置了大容量内存,拿DELL的R620来说在配置双路CPU下,其24个内存插槽,支持的内存高达960GB.对于ECC,REG这 ...
- ajax 向后台数组解决;
$.ajax({ traditional: true//这个设置为true,data:{"steps":["qwe","asd"," ...
- Octave 软件的安装
每次安装软件都感觉是一种心痛的历程.下载安装,然后就跳出一堆的错误,之后就各种百度求救,然后就搞了大半天,有时候还搞不定. 最后,搞定的时候发现,原来这么简单,结果时间就这样浪费了,所以还是把这个过程 ...
- NOIP2017到都不签签记
day 0: 校内开运动会,但是还是在机房学习了一天. 感觉上并没有多大用处,主要只是活跃一下思维而已. 然后就晚上上车出发去酒店. 下了个游戏王. 晚上叫了波宅急送,然后硬是腐了一个晚上. day ...
- 日志lombok插件安装及配置
安装lombok插件 下载Lombok.jar http://projectlombok.googlecode.com/files/lombok.jar 运行Lombok.jar java -jar ...
- vue 使用 element ui动态添加表单
html部分 <div class="hello"> <el-form :model="dynamicValidateForm" ref=&q ...
- Linux环境变量的种类
按环境变量的生存周期来划分,Linux的环境变量可分为两类: 1永久的:需要修改配置文件,变量永久生效. 2临时的:使用export命令行声明即可,变量在关闭shell时失效.