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

思路:有重复数字的情况,之前在Subsets II,我们采取的是在某一个递归内,用for循环处理所有重复数字。这里当然可以将数组排序,然后使用该方法。

而另一种方法是不排序,在一个递归内申请一个set,用来判断该数字是否已经在当前depth出现过:如果nums[depth]出现过,那么说明这个前缀在之前的遍历已存在,不需要再进行讨论。

class Solution {
public:
vector<vector<int> > permuteUnique(vector<int> &num) {
result.clear();
dfs(num, );
return result;
}
void dfs(vector<int> num, int depth)
{
if(depth == num.size()-)
{
result.push_back(num);
return;
} dfs(num,depth+);
set<int> flag; //用来判断当前数字是否在depth位置出现过
flag.insert(num[depth]);
int temp = num[depth];
for(int i = depth+; i< num.size(); i++)
{
if(flag.find(num[i])!=flag.end()) continue;
flag.insert(num[i]);
num[depth]=num[i];
num[i] = temp;
dfs(num,depth+);
num[i]=num[depth];
num[depth]=num[i];
}
}
private:
vector<vector<int> > result;
};

思路II:同样可以用insertion sort的方法

碰到相同元素,break for循环。注意不是continue,因为假设要插入的元素nums[i]与result[resultIndex][k]相同, 那么这个result[resultIndex][k]已经在上一轮的时候,对{0...k-1}的位置已经做过插入排序。如果再进行插入,会造成相同的前缀,导致重复result。

class Solution {
public:
vector<vector<int>> permuteUnique(vector<int>& nums) {
int size = nums.size();
int resultSize;
int resultIndex;
int count;
vector<vector<int>> result;
vector<int> resultItem(,nums[]);
result.push_back(resultItem);
for(int i = ; i <size; i++){ //nums[i] is the num to insert
resultSize = result.size(); //resultSize in the preceeding insert iterate
for(int j = ; j < resultSize; j++){ //iterate the array to do insertion
result[j].push_back(nums[i]);
resultIndex = j;
for(int k = i-; k >=; k--){ //like insertion sort, adjust forward
if(nums[i]==result[resultIndex][k]) break; //equal element, don't insert
result.push_back(result[resultIndex]);
result[result.size()-][k+] = result[resultIndex][k];
result[result.size()-][k] = result[resultIndex][k+];
resultIndex = result.size()-;
}
}
}
return result;
}
};

47. Permutations II (Back-Track, Sort)的更多相关文章

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

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

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

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

  3. 【LeetCode】47. Permutations II

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

  4. leetCode 47.Permutations II (排列组合II) 解题思路和方法

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

  5. [LeetCode] 47. Permutations II 全排列之二

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

  6. [leetcode] 47. Permutations II

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

  7. LeetCode 【47. Permutations II】

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

  8. 47. Permutations II

    题目: Given a collection of numbers that might contain duplicates, return all possible unique permutat ...

  9. 47. Permutations II(medium, backtrack, 重要, 条件较难思考)

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

随机推荐

  1. 使用file_get_contents提交http post

    以前使用curl获取需要登陆内容的文章,但其实,自5.0开始,使用file_get_contents就可以完成.(前提是开启了allow_url_fopen),下面以一个简单的例子说明一下:1.先看一 ...

  2. (转)高并发Web服务的演变——节约系统内存和CPU

    一.越来越多的并发连接数 现在的Web系统面对的并发连接数在近几年呈现指数增长,高并发成为了一种常态,给Web系统带来不小的挑战.以最简单粗暴的方式解决,就是增加Web系统的机器和升级硬件配置.虽然现 ...

  3. [置顶] VS 2017 众多重构插件

    孙广东  2017.7.22 http://blog.csdn.NET/u010019717 1.没有任何插件的情况下:  (就是Ctrl + .)   注意:这个.  要是英文的才行! 右键菜单也是 ...

  4. 【转载】你真的会浮点数与整型数的"互转"吗?

    看了标题,你是不是觉得这TM是哪个iOS彩笔写的入门文章.好的,那咱们先来看看几个例题,看看你有没有白白点进来! int main() { float a = -6.0; int *b = & ...

  5. java面试Linux常用命令使用方法大全

    1.# 表示权限用户(如:root),$ 表示普通用户   开机提示:Login:输入用户名   password:输入口令   用户是系统注册用户成功登陆后,可以进入相应的用户环境.   退出当前s ...

  6. SqlServer一些常用函数(持续更新。。。)

    1. 字符串拼接: + 拼接 SELECT 'AA' + 'BB' A //AABB在2012版本sqlserver之后,可以使用cancat进行字符串拼接了. 2. 判断是否为空,并取另外的值 :I ...

  7. 企业web管理软件开发平台

    可以自定义拖动记忆位置的part桌面 权限(功能菜单查看范围)设置界面 权限(人员数据查看范围)设置界面 菜单排序和名称设置界面 业务模块 自定义字段界面 业务功能  列表界面 业务功能   查询界面 ...

  8. jquery on() bind()绑定的点击事件在js动态新添加的元素生效

    方法一:$('.class').on("click",function(){……}); 相当于 $('.class').bind("click",functio ...

  9. CentOS7 tomcat systemctl脚本

    1,centos7 使用 systemctl 替换了 service命令 参考:redhat文档: https://access.redhat.com/documentation/en-US/Red_ ...

  10. 你正在从一个声称代表如下的证书颁发机构安装证书 alipay truest network,希望能知道程序是怎么实现的或相关资料

    你正在从一个声称代表如下的证书颁发机构安装证书 alipay truest network,希望能知道程序是怎么实现的或相关资料