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. kubernetes进阶(05)kubernetes的命令

    在Kubernetes中,Node.Pod.Replication Controller.Service等概念都可以看作一种资源对象,通过Kubernetes提供的Kubectl工具或者API调用进行 ...

  2. java将一个正整数分解质因数。例如:输入90,打印出90=2*3*3*5。

    首先我们的算法是:例如 输入的是 90 1.找到90的最小公约数(1除外)是 2 2.然后把公约数 2 输出 3.接着用 90 / 2 = 45 (如果这里是素数,就结束,否则继续找最小公约数) 4. ...

  3. 项目实战15—企业级堡垒机 jumpserver

    本文收录在Linux运维企业架构实战系列 环境准备 系统:CentOS 7 IP:192.168.10.101 关闭selinux 和防火墙 # CentOS $ setenforce # 可以设置配 ...

  4. Docker学习(1)安装

    1. Docker简介 Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的 Linux 机器上,也可以实现虚拟化.容器是完全使用沙箱 ...

  5. angular中的路径问题

    我们在写项目时会遇到启动页调到引导页,引导页再调到首页, 那我们在用angular框架写这种东西的时候如果我们不细心的话就会遇到问题, 比如说找不到引导页的图片等等. 那我们怎么解决这个问题呢? 首先 ...

  6. Java-NIO(五):通道(Channel)的数据传输与内存映射文件

    通道(Channel)的数据传输(采用非直接缓冲区) @Test public void testChannel() throws IOException { FileInputStream file ...

  7. JSON.stringify()和JSON.parse()分别是什么

    JSON.stringify() 从一个对象中解析出字符串 JSON.stringify({"a":"1","b":"2" ...

  8. js 常用数组和字符串方法

    javascript数组与字符串常用方法总结 最近在梳理js的基础,首先从数组和字符串开始. string 常用方法: 1.substring(start开始位置的索引,end结束位置索引) 截取的位 ...

  9. 如何在现有的 Web 应用中使用 ReactJS

    原文:How to Sprinkle ReactJS into an Existing Web Application 译者:nzbin 当我们学习一项新技术,可能是一个 JavaScript 框架, ...

  10. iOS10 越狱, openSSH

    iOS 10 已经可以越狱, 不过比较蛋疼的是非完美越狱,每次重启都要从新越狱. 感兴趣的同学可以尝试一下,本人使用同步推上的教程,亲测可用. 越狱完后想安装OpenSSH, 在Cydia上搜索安装, ...