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. 20181009-7 选题 Scrum立会报告+燃尽图 06

    Scrum立会报告+燃尽图(06)选题 此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2196 一.小组介绍 组长:刘莹莹 ...

  2. JSP乱码(小记)

    Post提交乱码: 设置请求的编码方式: request.setCharacterEncoding("utf-8"); 设置响应的编码方式: response.setCharact ...

  3. OMAP4之DSP核(Tesla)软件开发学习(三)使能DSP核

    目标: 1.编译或直接获取DSP映像tesla-dsp.bin. 2.boot并使能DSP核(这个可以看启动打印或者进入/d/remoteproc/omap-rproc.0/参看dsp调试信息).   ...

  4. 工作中比较重要的经验分享-2016-bypkm

    工作中总有一些经验能让人记忆深刻,能让人终生受用,相比技术而言,经验是宝贵的.无价的.在我的博客中,主要是技术类的博文,那些东西是相对死板的,价值也相对低廉.今天就记录一下我在工作中一次比较重要的经验 ...

  5. Jenkins配置slave遇到“无法启动该应用程序”的问题

    飞测说:最近在负责持续集成相关的工作,我们用的是jenkins+svn+maven+sonar, 今天在用slave这块出现了一个问题,排查了好久才解决,踩过的坑,现在和大家一起看看,希望对大家有帮助 ...

  6. BZOJ2121: 字符串游戏(DP)(字符串删单词,求最多可以删去多少)

    2121: 字符串游戏 Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 672  Solved: 376[Submit][Status][Discuss ...

  7. CF1061F:Lost Root(交互&概率)

    The graph is called tree if it is connected and has no cycles. Suppose the tree is rooted at some ve ...

  8. 重新学习之spring第四个程序,整合struts2+hibernate+spring

    第一步:导入三大框架的jar包(struts2.3.16.1+hibernate3.2+spring3.2.4) 第二步:编写web.xml 和struts.xml和applicationContex ...

  9. wpf 客户端【JDAgent桌面助手】开发详解(一)主窗口 圆形菜单

    目录区域: wpf 客户端[JDAgent桌面助手]业余开发的终于完工了..晒晒截图wpf 客户端[JDAgent桌面助手]开发详解-开篇 内容区域: 这里开始主窗口 圆形菜单制作的过程,首先请大家看 ...

  10. /sys/kernel/debug/usb/devices解析

    1.概述 USB设备通过debugfs导出/sys/kernel/debug/usb/devices显示内核已知的每个USB设备及其配置描述符.此文件对于用户模式下的状态查看工具非常方便,可以扫描文本 ...