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

  1. class Solution {
  2. private:
  3. vector<vector<int> > ret;
  4. public:
  5. void perm(vector<int> &num,int i,int len){
  6. if(i==len){
  7. ret.push_back(num);
  8. return;
  9. }
  10. for(int j=i;j<len;++j){
  11. if(!isSwap(num,i,j))
  12. continue;
  13. swap(num[i],num[j]);
  14. perm(num,i+,len);
  15. swap(num[j],num[i]);
  16. }
  17. }
  18. bool isSwap(vector<int>& num, int i, int j) {
  19. while (num[i] != num[j] && i < j) i++;
  20. if (i == j) return true;
  21. else return false;
  22. }
  23. vector<vector<int> > permuteUnique(vector<int> &num) {
  24. int len=num.size();
  25. ret.clear();
  26. sort(num.begin(), num.end());
  27. perm(num,,len);
  28. return ret;
  29. }
  30. };

参考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. Django中间件分析

    SessionMiddleware 浏览器会发送包含SESSION_COOKIE_NAME的的Cookie 中间件从django_session中按照SESSION_COOKIE_NAME取出存入的s ...

  2. k8s 内部各个部件运转

    Master节点部署的都是kubernetes的核心模块APIServer提供资源操作的唯一入口,并且提供认证/授权/kubernets的访问控制可以通过kubectl和自己开发的客户端,通过http ...

  3. AppbarLayout的简单用法

    在许多App中看到, toolbar有收缩和扩展的效果, 例如:   appbar.gif 要实现这样的效果, 需要用到: CoordinatorLayout和AppbarLayout的配合, 以及实 ...

  4. PAT甲级——A1038 Recover the Smallest Number

    Given a collection of number segments, you are supposed to recover the smallest number from them. Fo ...

  5. Spring Boot入门样例-001-Java和Maven安装配置

    Spring Boot入门样例-001-Java和Maven安装配置 本文说明Java和Maven在windows下的安装和配置 前言 本Spring Boot入门样例准备工作参考: Spring B ...

  6. 建造者模式(Builder)(生成器模式)(框架化)

    建造者模式将一个复杂对象的构建与其表示分离. 将复杂对象进行框架化,将同类的对象编造进同一个制造流程.同类·对象会有一样的框架. 而由于各部分的实现细节有所不同,所生产出来的产品会有所不同.从而有不同 ...

  7. 谷歌浏览器flash被禁用解决方法

    谷歌浏览器访问设置:chrome://settings/content/flash 把要启动flash插件的网址添加进去

  8. Spring AOP(转)

    原文:Spring实现AOP的4种方式 Spring AOP 详解 Spring实现AOP的4种方式 先了解AOP的相关术语:1.通知(Advice):通知定义了切面是什么以及何时使用.描述了切面要完 ...

  9. mysql 常用命令语法

    登录到mysql client 以windows下为例,打开cmd命令窗口,进入到mysql安装目录bin目录下,首先要启动mysql服务,执行命令: net start mysql,这里不需要分号. ...

  10. LINUX下C++编程如何获得某进程的ID

    #include <stdio.h> #include <stdlib.h> #include <unistd.h> using namespace std; pi ...