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. 新概念英语(1-57)An unusual day

    新概念英语(1-57)An unusually day What is Mr. Sawyer doing tonight? It is eight o'clock. The children go t ...

  2. XSS和CSRF的理解

    声明:转自 http://www.cnblogs.com/hyddd/archive/2009/04/09/1432744.html XSS攻击:跨站脚本攻击(Cross Site Scripting ...

  3. 一日一练-CSS-CSS 居中

    特别声明:此篇文章内容来源于@CHRIS COYIER 的Centering in CSS:A Complete Guide 子曰:CSS 居中是一个非常常见的问题,无论是在项目中,还是在各种面试资料 ...

  4. TSQL:A表字段与B表中的关联,关联条件中一列是随机关联的实现方式

    A表字段与B表中的关联,关联条件中一列是随机关联的实现方式 create table test( rsrp string, rsrq string, tkey string, distan strin ...

  5. Spring(一):eclipse上安装spring开发插件&下载Spring开发包

    eclipse上安装spring开发插件 1)下载安装插件包:https://spring.io/tools/sts/all 由于我的eclipse版本是mars 4.5.2,因此我这里下载的插件包是 ...

  6. [转]Python多进程并发操作中进程池Pool的应用

    Pool类 在使用Python进行系统管理时,特别是同时操作多个文件目录或者远程控制多台主机,并行操作可以节约大量的时间.如果操作的对象数目不大时,还可以直接使用Process类动态的生成多个进程,十 ...

  7. Spring(5)——Spring 和数据库编程

    传统 JDBC 回顾 JDBC 我们一定不陌生,刚开始学习的时候,我们写过很多很多重复的模板代码: public Student getOne(int id) { String sql = " ...

  8. 1028阿里RDS如何恢复云数据库MySQL的备份文件到自建数据库

    参照 https://help.aliyun.com/knowledge_detail/41817.html 恢复云数据库MySQL的备份文件到自建数据库 更新时间:2017-07-27 14:52: ...

  9. Vue还有这种操作?浅析几个新手常常忽略的API

    一:实现子组件与父组件双向绑定的"sync": 一般来说,我们实现父子组件值的传递通常使用的是[props]和自定义事件[$emit].父组件通过[props]将值传给子组件,子组 ...

  10. 调用Kubernetes API操作Kubernetes

    准备工作 首先要准备一个1.5+版本的Kubernetes,并且开放了API Server的http访问端口8080.本文使用的是1.10的版本,没有环境的可以参考我上一篇文章<在CentOS ...