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. (转)nodejs

    在写node.js代码时,我们经常需要自己写模块(module).同时还需要在模块最后写好模块接口,声明这个模块对外暴露什么内容.实际上,node.js的模块接口有多种不同写法.这里作者对此做了个简单 ...

  2. pycharm 安装与激活

    ---恢复内容开始--- 环境:Windows 专业版 1.下载安装 1.到官网下载专业版(专业版功能更全(但要激活码),社区版免费) 2.下载完后双击.exe 文件进行安装 NEXT 下一步 NEX ...

  3. org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.connections.spi.ConnectionProvider]

    从hibernate3升级到4应该会遇到 应该添加引用 <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-c3p0 ...

  4. IndentationError: expected an indented block错误

    Python语言是一款对缩进非常敏感的语言,给很多初学者带来了困惑,即便是很有经验的python程序员,也可能陷入陷阱当中.最常见的情况是tab和空格的混用会导致错误,或者缩进不对,而这是用肉眼无法分 ...

  5. HTML入门:Tag学习

    即使 <br> 在所有浏览器中的显示都没有问题,使用 <br /> 也是更长远的保障. 标签 描述 <html> 定义 HTML 文档. <body> ...

  6. Ubuntu 18.04 美化

    Ubuntu 18.04 美化 sudo apt install gnome-tweak-tool sudo apt install gnome-shell-extensions sudo apt i ...

  7. [Array]414. Third Maximum Number

    Given a non-empty array of integers, return the third maximum number in this array. If it does not e ...

  8. 你不知道的javascript -- 数据类型

    1. 数据类型 在js中有7中数据类型 其中6种是基本类型 包括 null, undefined, boolean, number, string和symbol,还有一种是引用类型object 但是判 ...

  9. LintCode 合并两个排序

    将两个排序链表合并为一个新的排序链表 样例 给出 1->3->8->11->15->null,2->null, 返回1->2->3->8-> ...

  10. GIT → 00:GIT学习大纲

    1. 学习版本控制的原因 1.1 没有版本控制出现的问题 1.2 版本控制的简介 1.3 版本控制工具 2. Git 和 Svn 比较 2.1 SVN介绍 2.1.1 SVN简介 2.1.2 SVN基 ...