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

本题有重复元素,条件较难思考,做这个题费了小劲.

总体框架是应用 backtrack 解决.

样例:[1 1 2] -> [1 2 1] -> [2 1 1]

设定一个 vector<bool> used(A.size(), false); 表示哪些元素用过, 用过的用true表示.

[1 1 2] -> [1 2 1] dfs到这种状态后, i 将要为 1, 就是下面的样子.

[1 1 2] 此时所有的 used[0 - 2] 都为 false.
i

if (i > 0 && A[i - 1] == A[i] && !used[i - 1]) continue; // <--这句话,想不出来啊

若无上面那个判断条件(有这个标志的那行 '<--'),将会再次产生下面的序列:

[1 1 2], 这里的第一个1是上面i=1所指的元素,第二个1是i=0的元素.

为防止这种事的发生,就应该跳过i=1的那个元素, 仔细观察, 发现:

  1. i > 0;
  2. A[i] = A[i-1];
  3. A[i-1] = false;

满足上述3条的元素, 就应跳过!

经验:
想写清楚条件,必须把简单例子用笔纸走一遍;
找出自己跑的结果与正确结果不同之处;
针对错误点, 设置if()语句,避免之!

自己代码:

// e.g.
// [1 1 2] -> [1 2 1] -> [2 1 1]
vector<vector<int>> permuteUnique(vector<int>& A) {
sort(A.begin(), A.end());
vector < vector<int> > res;
vector<int> temp; // 用 used[0-2], true 表用过
vector<bool> used(A.size(), false);
backtrack(res, temp, A, used);
return res;
} void backtrack(vector<vector<int> >& res, vector<int>& temp, vector<int>& A,
vector<bool> used) {
if (temp.size() == A.size())
res.push_back(temp);
else {
for (int i = 0; i < A.size(); i++) {
if (used[i] == true) {
continue;
}
if (i > 0 && A[i - 1] == A[i] && !used[i - 1])
continue; // <--这句话,想不出来啊
// [1 1 2] -> [1 2 1] dfs到这种状态后, i 将要为 1, 就是下面的样子. // [1 1 2] 此时所有的 used[0 - 2] 都为 false.
// i
// 若无上面那个判断条件(有这个标志的那行 '<--'),将会再次产生下面的序列:
// [1 1 2], 这里的第一个1是上面i=1所指的元素,第二个1是i=0的元素
// 为防止这种事的发生,就应该跳过i=1的那个元素, 仔细观察, 发现:
// 1. i > 0;
// 2. A[i] = A[i-1];
// 3. A[i-1] = false;
// 满足上述3条的元素,就应跳过!
//
// 经验:
// 想写清楚条件,必须把简单例子用笔纸走一遍;
// 找出自己跑的结果与正确结果不同之处;
// 针对错误点,设置if()语句,避免之! temp.push_back(A[i]);
used[i] = true;
backtrack(res, temp, A, used);
used[i] = false;
temp.pop_back();
}
}
}

47. Permutations II(medium, backtrack, 重要, 条件较难思考)的更多相关文章

  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. 47. Permutations II (Back-Track, Sort)

    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. (待解决,效率低下)47. Permutations II C++回溯法

    思路是在相似题Permutations的基础上,将结果放到set中,利用set容器不会出现重复元素的特性,得到所需结果 但是利用代码中的/* */部分通过迭代器遍历set将set中的元素放在一个新的v ...

  8. 47. Permutations II (JAVA)

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

  9. 【LeetCode】47. Permutations II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:递归 方法二:回溯法 日期 题目地址:htt ...

随机推荐

  1. .net 4种单例模式

    转载: https://www.cnblogs.com/dreign/archive/2012/05/08/2490212.html using System; using System.Collec ...

  2. loadrunner录制时web时,安全证书问题

    测试环境:win7+LoadRunner11+ie9 遇到的问题:用LoadRunner录制时,打开百度,总是报安全证书问题,如图所示 解决方法:Tools——Recording Options——p ...

  3. Django中自定义过滤器的使用

    我在这里做的是: 从数据库查出id递增的一些信息,展示在前台. 编写一个过滤器判断查出数据的id是偶数的返回True 奇数返回False 1 创建项目,创建应用,注册应用,配置settings.py文 ...

  4. SSM中的登陆验证码

    @Autowired private Producer captchaProducer = null; /** * 后台登录验证码 * @param request * @param response ...

  5. centos7 yum相关的常用命令

    [root@mini1 ~]# history |grep yum 40 yum repolist 42 cd /etc/yum.repos.d/ 49 yum clean all 50 yum re ...

  6. awk、变量、运算符、if多分支

    awk.变量.运算符.if多分支 awk: 语法 awk [options] 'commands' files option -F 定义字段分隔符,默认的分隔符是连续的空格或制表符 使用option中 ...

  7. JavaEE EL & JSTL 学习笔记

    1. EL表达式(特别重要)

  8. CSS Box Model 盒子模型

    1. 介绍 1.1 什么是 Box Model 在HTML中的每个element(元素)都可以看作一个矩形的盒子,矩形从内到外依次由元素的内容(content).内边距(padding).边框(bor ...

  9. disabled OR readonly

    1.对元素设置disabled以及readonly属性 $("#uid").attr("disabled",true); $("#uid") ...

  10. jmeter出现卡死或内存溢出的解决方案

    故事背景:在初次使用jmeter的时候,把线程设置较大值的时候,jmeter工具很容易就卡死了,导致每次做压测的时候都无法顺利完成,非常的闹心,通过各种方法寻找解决方案,终于找到了一个比较靠谱的方法, ...