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. Odoo加载机制指导流程

    Odoo的启动通过openerp-server脚本完成,它是系统的入口. 然后加载配置文件openerp-server.conf 或者 openerp_serverrc: openerp-server ...

  2. leetcode 1078 Occurrences After Bigram

    lc1078 Occurrences After Bigram trim().split()将原字符串转换成words数组 依次匹配first和second,若两者都能匹配上,则下一个单词为third ...

  3. HZOI20190817模拟24

    题面:https://www.cnblogs.com/Juve/articles/11369181.html A:Star Way To Heaven 考场上以为只能走直线,于是挂掉了. 有一种方法是 ...

  4. 19-10-15-W

    暴力终于不跪了$\text{QvQ}$ z总j结 考试开始看到几个大字:Day1 Happy-(××终于不用爆〇了哈哈哈哈!!) 开T1.一看,不是在线仙人球嵌套动态网络路径剖分优化的分支定界贪心剪枝 ...

  5. sqlserver 创建用户 sp_addlogin

    创建新的 Microsoft® SQL Server™ 登录,使用户得以连接使用 SQL Server 身份验证的 SQL Server 实例.  语法: sp_addlogin [ @loginam ...

  6. 《DSP using MATLAB》Problem 7.32

    代码: %% ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ %% Output In ...

  7. lost connection to MySQL server at waiting for initial communication packet,system error:o

    1 可以先测试mysql本地连接石否正常 2 正常的话查看远程连接的IP在mysql中是否有权限 GRANT ALL PRIVILEGES ON *.* TO 'root'@'%'   IDENTIF ...

  8. python 当文件目录不存在时,如何自动创建

    import os if not os.path.exists('foldername'): os.mkdir('foldername')

  9. Redis 核心

    一.Redis单机多实例原理 每个实例对应不同的配置文件,配置文件对应不同的端口.数据库文件位置.日志位置. 二.Redis单实例多数据库 每个Redis实例都有16个数据库,下标从0-15,当 se ...

  10. 跟我一起做一个vue的小项目(二)

    这个vue项目是紧跟着之前的项目跟我一起做一个vue的小项目(一)来的. 我继续后面的开发(写的比较粗糙,边学边记录) 下图是header头部的样式 header组件内容如下 //header.vue ...