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],
[2,1,1]
]

分析: 全组合的思想,保证start和end之间交换的时候中间没有与end相同的数字

class Solution {
public:
void swap(int i, int j, vector<int>& nums){
int temp =nums[i];
nums[i] = nums[j];
nums[j]= temp;
}
bool isSwap(int start, int end, vector<int>& nums){
for(; start<end; start++)
if(nums[start] == nums[end])
return false;
return true;
} void allRange(int start, vector<int>& nums, vector<vector<int>>& res)
{
if(start==nums.size()-1)
return;
for(int i =start; i<nums.size(); i++){
if(isSwap(start,i,nums))
{
swap(start, i, nums);
if(start!=i)
res.push_back(nums);
allRange(start+1, nums, res);
swap(i, start, nums);
} }
}
vector<vector<int>> permuteUnique(vector<int>& nums) {
vector<vector<int>> res;
if(nums.size()==0)
return res;
res.push_back(nums);
allRange(0, nums, res);
return res;
}
};

  

Permutations II的更多相关文章

  1. 【leetcode】Permutations II

    Permutations II Given a collection of numbers that might contain duplicates, return all possible uni ...

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

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

  3. leetcode总结:permutations, permutations II, next permutation, permutation sequence

    Next Permutation: Implement next permutation, which rearranges numbers into the lexicographically ne ...

  4. LeetCode46,47 Permutations, Permutations II

    题目: LeetCode46 I Given a collection of distinct numbers, return all possible permutations. (Medium) ...

  5. leetcode Permutations II 无重全排列

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

  6. [Leetcode][Python]47: Permutations II

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 47: Permutations IIhttps://oj.leetcode. ...

  7. Permutations,Permutations II,Combinations

    这是使用DFS来解数组类题的典型题目,像求子集,和为sum的k个数也是一个类型 解题步骤: 1:有哪些起点,例如,数组中的每个元素都有可能作为起点,那么用个for循环就可以了. 2:是否允许重复组合 ...

  8. leetcode46. Permutations 、47. Permutations II、 剑指offer字符串的排列

    字符串排列和PermutationsII差不多 Permutations第一种解法: 这种方法从0开始遍历,通过visited来存储是否被访问到,level代表每次已经存储了多少个数字 class S ...

  9. Permutations II - LeetCode

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

  10. 【LeetCode】47. Permutations II

    Permutations II Given a collection of numbers that might contain duplicates, return all possible uni ...

随机推荐

  1. Vertica 业务用户指定资源池加载数据

    之前在"Vertica 安装,建库,新建测试用户并授予权限,建表,入库"这篇文章也简单介绍过入库部分的内容. 但之前测试用例若用于生产环境有明显的局限性: 1.是用dbadmin管 ...

  2. Vertica DBD 分析优化设计

    DBD = Database Designer,是Vertica数据库优化中最主要的原生工具. 首先运行admintools工具,按下面步骤依次执行: 1.选择"6 Configuratio ...

  3. 做个简单的RSS订阅(ASP.NET Core),节省自己的时间

    0x01 前言 因为每天上下班路上,午休前,都是看看新闻,但是种类繁多,又要自己找感兴趣的,所以肯定会耗费不少时间. 虽说现在有很多软件也可以订阅一些自己喜欢的新闻,要安装到手机,还是挺麻烦的.所以就 ...

  4. sqlserver 游标的使用

    declare @temp_temp uniqueidentifier--临时变量 DECLARE aaa CURSOR for select Id from A ------------------ ...

  5. 关系数据库SQL之可编程性触发器

    前言 前面关系数据库SQL之可编程性函数(用户自定义函数)一文提到关系型数据库提供了可编程性的函数.存储过程.事务.触发器及游标,前文已介绍了函数.存储过程.事务,本文来介绍一下触发器的使用.(还是以 ...

  6. WPF 自定义Grid双击事件

    先设置Grid_MouseDown事件函数,然后在函数内增加如下代码 ) { //双击执行 } 更改数字可以区别单击和双击 感谢@一 定 会 去 旅 行

  7. python之最强王者(5)——Nunber(数字)

    1.Python Number(数字) Python Number 数据类型用于存储数值. 数据类型是不允许改变的,这就意味着如果改变 Number 数据类型的值,将重新分配内存空间. 以下实例在变量 ...

  8. 《连载 | 物联网框架ServerSuperIO教程》- 8.单例通讯模式开发及注意事项

    1.C#跨平台物联网通讯框架ServerSuperIO(SSIO)介绍 <连载 | 物联网框架ServerSuperIO教程>1.4种通讯模式机制. <连载 | 物联网框架Serve ...

  9. 服务发现与健康监测框架Consul-DNS转发的应用

    关于Consul Consul是一个提供服务注册与发现,健康监测,Key/Value存储以及多数据中心存储的分布式框架.官网地址是https://www.consul.io/,公司初步应用后我们老大觉 ...

  10. Jquery元素选取、常用方法

    一:常用的选择器:(李昌辉) 基本选择器 $("#myDiv") //匹配唯一的具有此id值的元素 $("div") //匹配指定名称的所有元素 $(" ...