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].

Hide Tags

Backtracking

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 去掉重复的全排列的更多相关文章

  1. 047 Permutations II 有重复数字的全排列

    给定一个可能包含重复数字的集合,返回所有可能的不同全排列.例如,[1,1,2] 有以下不同全排列:[  [1,1,2],  [1,2,1],  [2,1,1]] 详见:https://leetcode ...

  2. leetcode Permutations II 无重全排列

    作者:jostree  转载请注明出处 http://www.cnblogs.com/jostree/p/4051169.html 题目链接:leetcode Permutations II 无重全排 ...

  3. Leetcode之回溯法专题-47. 全排列 II(Permutations II)

    Leetcode之回溯法专题-47. 全排列 II(Permutations II) 给定一个可包含重复数字的序列,返回所有不重复的全排列. 示例: 输入: [1,1,2] 输出: [ [1,1,2] ...

  4. [Swift]LeetCode47. 全排列 II | Permutations II

    Given a collection of numbers that might contain duplicates, return all possible unique permutations ...

  5. LeetCode 47. 全排列 II(Permutations II)

    题目描述 给定一个可包含重复数字的序列,返回所有不重复的全排列. 示例: 输入: [1,1,2] 输出: [ [1,1,2], [1,2,1], [2,1,1] ] 解题思路 类似于LeetCode4 ...

  6. Leetcode47. Permutations II全排列2

    给定一个可包含重复数字的序列,返回所有不重复的全排列. 示例: 输入: [1,1,2] 输出: [ [1,1,2], [1,2,1], [2,1,1] ] 在全排列1题目的基础上先排序,目的是把相同的 ...

  7. LeetCode:Permutations, Permutations II(求全排列)

    Permutations Given a collection of numbers, return all possible permutations. For example, [1,2,3] h ...

  8. Permutations II - LeetCode

    目录 题目链接 注意点 解法 小结 题目链接 Permutations II - LeetCode 注意点 不确定有几种排列 解法 解法一:因为有重复的数字所以排列的个数不确定几个,一直生成新的排列直 ...

  9. leetcode47. Permutations II

    leetcode47. Permutations II 题意: 给定可能包含重复的数字的集合,返回所有可能的唯一排列. 思路: 这题全排列两种写法. 用hash表记录已经visit的数字,虽然看起来很 ...

随机推荐

  1. PostgreSQL的架构

    是最先进的数据库.他的第一个版本在1989年发布,从那时开始,他得到了很多扩展.根据db-enginers上的排名情况,PostgreSQL目前在数据库领域排名第四. 本篇博客,我们来讨论一下Post ...

  2. final,finally和finalize之间的区别

    (1)final用于声明属性,方法和类,分别表示属性不可变,方法不可覆盖,类不可继承.内部类要访问局部变量,局部变量必须定义成final类型,比如一段代码 (2)finally是异常处理语句结构的一部 ...

  3. 如何设置td中溢出内容的隐藏显示

    <style type="text/css"> table { table-layout:fixed; } td { overflow:hidden; word-bre ...

  4. Spring注解驱动开发(二)-----生命周期、属性赋值

    bean的生命周期 bean的生命周期:bean创建---初始化----销毁的过程容器管理bean的生命周期:我们可以自定义初始化和销毁方法:容器在bean进行到当前生命周期的时候来调用我们自定义的初 ...

  5. 常用 docker 容器 使用

    mongo: 单点 docker run -idt --name=mongo --restart=always -p : -v /home/hylas/opt/mongo/data:/data/db ...

  6. redis中重启和停止服务

    首先需要打开两个终端,一个是服务端,一个是客户端 1.开启服务端 redis-server 2.开启客户端 redis-cli 关闭双方之间的连接: 在客户端中输入:redis-cli shutdow ...

  7. Google 和 微软,就不是一个重量级的

    看微软的 WinCE 模拟器,做得带模带样   Google 也不是新来的,但是 Android 模拟器,配置一下都那么费事   这就是差距,Google 比 微软 差了好几级

  8. git 远程服务器创建项目自动化部署、克隆推送免密码

    1.用git用户 在git目录下 创建裸仓库 git init --bare project_01.git 2.在裸仓库的 hooks目录下创建 post-receive 文件775 3.post-r ...

  9. Leetcode131. Palindrome Partitioning分割回文串

    给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串. 返回 s 所有可能的分割方案. 示例: 输入: "aab" 输出: [ ["aa",&quo ...

  10. 理解 Python 语言中的 defaultdict

    众所周知,在Python中如果访问字典中不存在的键,会引发KeyError异常(JavaScript中如果对象中不存在某个属性,则返回undefined).但是有时候,字典中的每个键都存在默认值是非常 ...