[LeetCode] Permutations II 全排列之二
Given a collection of numbers that might contain duplicates, return all possible unique permutations.
Example:
Input: [1,1,2]
Output:
[
[1,1,2],
[1,2,1],
[2,1,1]
]
这道题是之前那道 Permutations 的延伸,由于输入数组有可能出现重复数字,如果按照之前的算法运算,会有重复排列产生,我们要避免重复的产生,在递归函数中要判断前面一个数和当前的数是否相等,如果相等,且其对应的 visited 中的值为1,当前的数字才能使用(下文中会解释这样做的原因),否则需要跳过,这样就不会产生重复排列了,代码如下:
解法一:
class Solution {
public:
vector<vector<int>> permuteUnique(vector<int>& nums) {
vector<vector<int>> res;
vector<int> out, visited(nums.size(), );
sort(nums.begin(), nums.end());
permuteUniqueDFS(nums, , visited, out, res);
return res;
}
void permuteUniqueDFS(vector<int>& nums, int level, vector<int>& visited, vector<int>& out, vector<vector<int>>& res) {
if (level >= nums.size()) {res.push_back(out); return;}
for (int i = ; i < nums.size(); ++i) {
if (visited[i] == ) continue;
if (i > && nums[i] == nums[i - ] && visited[i - ] == ) continue;
visited[i] = ;
out.push_back(nums[i]);
permuteUniqueDFS(nums, level + , visited, out, res);
out.pop_back();
visited[i] = ;
}
}
};
在使用上面的方法的时候,一定要能弄清楚递归函数的 for 循环中两个 if 的剪枝的意思。在此之前,要弄清楚 level 的含义,这里用数组 out 来拼排列结果,level就是当前已经拼成的个数,其实就是 out 数组的长度。我们看到,for 循环的起始是从0开始的,而本题的解法二,三,四都是用了一个 start 变量,从而 for 循环都是从 start 开始,一定要分清楚 start 和本解法中的 level 的区别。由于递归的 for 都是从0开始,难免会重复遍历到数字,而全排列不能重复使用数字,意思是每个 nums 中的数字在全排列中只能使用一次(当然这并不妨碍 nums 中存在重复数字)。不能重复使用数字就靠 visited 数组来保证,这就是第一个 if 剪枝的意义所在。关键来看第二个 if 剪枝的意义,这里说当前数字和前一个数字相同,且前一个数字的 visited 值为0的时候,必须跳过。这里的前一个数 visited 值为0,并不代表前一个数字没有被处理过,也可能是递归结束后恢复状态时将 visited 值重置为0了,实际上就是这种情况,下面打印了一些中间过程的变量值,如下所示:
level = 0, i = 0 => out: {}
level = 1, i = 0 => out: {1 } skipped 1
level = 1, i = 1 => out: {1 }
level = 2, i = 0 => out: {1 2 } skipped 1
level = 2, i = 1 => out: {1 2 } skipped 1
level = 2, i = 2 => out: {1 2 }
level = 3 => saved {1 2 2}
level = 3, i = 0 => out: {1 2 2 } skipped 1
level = 3, i = 1 => out: {1 2 2 } skipped 1
level = 3, i = 2 => out: {1 2 2 } skipped 1
level = 2, i = 2 => out: {1 2 2 } -> {1 2 } recovered
level = 1, i = 1 => out: {1 2 } -> {1 } recovered
level = 1, i = 2 => out: {1 } skipped 2
level = 0, i = 0 => out: {1 } -> {} recovered
level = 0, i = 1 => out: {}
level = 1, i = 0 => out: {2 }
level = 2, i = 0 => out: {2 1 } skipped 1
level = 2, i = 1 => out: {2 1 } skipped 1
level = 2, i = 2 => out: {2 1 }
level = 3 => saved {1 2 2}
level = 3, i = 0 => out: {2 1 2 } skipped 1
level = 3, i = 1 => out: {2 1 2 } skipped 1
level = 3, i = 2 => out: {2 1 2 } skipped 1
level = 2, i = 2 => out: {2 1 2 } -> {2 1 } recovered
level = 1, i = 0 => out: {2 1 } -> {2 } recovered
level = 1, i = 1 => out: {2 } skipped 1
level = 1, i = 2 => out: {2 }
level = 2, i = 0 => out: {2 2 }
level = 3 => saved {1 2 2}
level = 3, i = 0 => out: {2 2 1 } skipped 1
level = 3, i = 1 => out: {2 2 1 } skipped 1
level = 3, i = 2 => out: {2 2 1 } skipped 1
level = 2, i = 0 => out: {2 2 1 } -> {2 2 } recovered
level = 2, i = 1 => out: {2 2 } skipped 1
level = 2, i = 2 => out: {2 2 } skipped 1
level = 1, i = 2 => out: {2 2 } -> {2 } recovered
level = 0, i = 1 => out: {2 } -> {} recovered
level = 0, i = 2 => out: {} skipped 2
注意看这里面的 skipped 1 表示的是第一个 if 剪枝起作用的地方,skipped 2 表示的是第二个 if 剪枝起作用的地方。我们主要关心的是第二个 if 剪枝,看上方第一个蓝色标记的那行,再上面的红色一行表示在 level = 1, i = 1 时递归调用结束后,恢复到起始状态,那么此时 out 数组中只有一个1,后面的2已经被 pop_back() 了,当然对应的 visited 值也重置为0了,这种情况下需要剪枝,当然不能再一次把2往里加,因为这种情况在递归中已经加入到结果 res 中了,所以到了 level = 1, i = 2 的状态时,nums[i] == nums[i-1] && visited[i-1] == 0 的条件满足了,剪枝就起作用了,这种重复的情况就被剪掉了。
还有一种比较简便的方法,在 Permutations 的基础上稍加修改,用 TreeSet 来保存结果,利用其不会有重复项的特点,然后在递归函数中 swap 的地方,判断如果i和 start 不相同,但是 nums[i] 和 nums[start] 相同的情况下跳过,继续下一个循环,参见代码如下:
解法二:
class Solution {
public:
vector<vector<int>> permuteUnique(vector<int>& nums) {
set<vector<int>> res;
permute(nums, , res);
return vector<vector<int>> (res.begin(), res.end());
}
void permute(vector<int>& nums, int start, set<vector<int>>& res) {
if (start >= nums.size()) res.insert(nums);
for (int i = start; i < nums.size(); ++i) {
if (i != start && nums[i] == nums[start]) continue;
swap(nums[i], nums[start]);
permute(nums, start + , res);
swap(nums[i], nums[start]);
}
}
};
对于上面的解法,你可能会有疑问,我们不是在 swap 操作之前已经做了剪枝了么,为什么还是会有重复出现,以至于还要用 TreeSet 来取出重复呢。总感觉使用 TreeSet 去重复有点耍赖,可能并没有探究到本题深层次的内容。这是很好的想法,首先尝试将上面的 TreeSet 还原为 vector,并且在主函数调用递归之前给 nums 排个序(代码参见评论区三楼),然后测试一个最简单的例子:[1, 2, 2],得到的结果为:
[[1,2,2], [2,1,2], [2,2,1], [2,2,1], [2,1,2]]
我们发现有重复项,那么剪枝究竟在做些什么,怎么还是没法防止重复项的产生!那个剪枝只是为了防止当 start = 1, i = 2 时,将两个2交换,这样可以防止 {1, 2, 2} 被加入两次。但是没法防止其他的重复情况,要闹清楚为啥,需要仔细分析一些中间过程,下面打印了一些中间过程的变量:
start = , i = => { }
start = , i = => { }
start = , i = => { }
start = => saved { }
start = , i = => { } skipped
start = , i = => { } -> { }
start = , i = => { }
start = , i = => { }
start = => saved { }
start = , i = => { } -> { }
start = , i = => { }
start = => saved { }
start = 1, i = 2 => {2 2 1} -> {2 1 2} recovered
start = 0, i = 1 => {2 1 2} -> {1 2 2} recovered
start = , i = => { } -> { }
start = , i = => { }
start = , i = => { }
start = => saved { }
start = , i = => { } -> { }
start = , i = => { }
start = => saved { }
start = , i = => { } -> { } recovered
start = , i = => { } -> { } recovered
问题出在了递归调用之后的还原状态,参见上面的红色的两行,当 start = 0, i = 2 时,nums 已经还原到了 {1, 2, 2} 的状态,此时 nums[start] 不等于 nums[i],剪枝在这已经失效了,那么交换后的 {2, 2, 1} 还会被存到结果 res 中,而这个状态在之前就已经存过了一次。
注意到当 start = 0, i = 1 时,nums 交换之后变成了 {2, 1, 2},如果能保持这个状态,那么当 start = 0, i = 2 时,此时 nums[start] 就等于 nums[i] 了,剪枝操作就可以发挥作用了。怎么才能当递归结束后,不还原成为交换之前的状态的呢?答案就是不进行还原,这样还是能保存为之前交换后的状态。只是将最后一句 swap(nums[i], nums[start]) 删掉是不行的,因为递归函数的参数 nums 是加了&号,就表示引用了,那么之前调用递归函数之前的 nums 在递归函数中会被修改,可能还是无法得到我们想要的顺序,所以要把递归函数的 nums 参数的&号也同时去掉才行,参见代码如下:
解法三:
class Solution {
public:
vector<vector<int>> permuteUnique(vector<int>& nums) {
vector<vector<int>> res;
sort(nums.begin(), nums.end());
permute(nums, , res);
return res;
}
void permute(vector<int> nums, int start, vector<vector<int>>& res) {
if (start >= nums.size()) res.push_back(nums);
for (int i = start; i < nums.size(); ++i) {
if (i != start && nums[i] == nums[start]) continue;
swap(nums[i], nums[start]);
permute(nums, start + , res);
}
}
};
好,再测试下 [1, 2, 2] 这个例子,并且把中间变量打印出来:
start = , i = => { }
start = , i = => { }
start = , i = => { }
start = => saved { }
start = , i = => { } skipped
start = , i = => { } -> { }
start = , i = => { }
start = , i = => { }
start = => saved { }
start = , i = => { } -> { }
start = , i = => { }
start = => saved { }
start = 1, i = 2 => {2 2 1} recovered
start = 0, i = 1 => {2 1 2} recovered
start = , i = => { } skipped
明显发现短了许多,说明剪枝发挥了作用,看上面红色部分,当 start = 0, i = 1 时,递归函数调用完了之后,nums 数组保持了 {2, 1, 2} 的状态,那么到 start = 0, i = 2 的时候,nums[start] 就等于 nums[i] 了,剪枝操作就可以发挥作用了。
这时候你可能会想,调用完递归不恢复状态,感觉怪怪的,跟哥的递归模版不一样啊,容易搞混啊,而且一会加&号,一会不加的,这尼玛谁能分得清啊。别担心,I gotcha covered! 好,既然还是要恢复状态的话,就只能从剪枝入手了,原来那种 naive 的剪枝方法肯定无法使用,矛盾的焦点还是在于,当 start = 0, i = 2 时,nums 被还原成了 start = 0, i = 1 的交换前的状态 {1, 2, 2},这个状态已经被处理过了,再去处理一定会产生重复,怎么才知道这被处理过了呢,当前的 i = 2,需要往前去找是否有重复出现,由于数组已经排序过了,如果有重复,那么前面数一定和当前的相同,所以用一个 while 循环,往前找和 nums[i] 相同的数字,找到了就停下,当然如果小于 start 了也要停下,那么如果没有重复数字的话,j 一定是等于 start-1 的,那么如果不等于的话,就直接跳过就可以了,这样就可以去掉所有的重复啦,参见代码如下:
解法四:
class Solution {
public:
vector<vector<int>> permuteUnique(vector<int>& nums) {
vector<vector<int>> res;
sort(nums.begin(), nums.end());
permute(nums, , res);
return res;
}
void permute(vector<int>& nums, int start, vector<vector<int>>& res) {
if (start >= nums.size()) res.push_back(nums);
for (int i = start; i < nums.size(); ++i) {
int j = i - ;
while (j >= start && nums[j] != nums[i]) --j;
if (j != start - ) continue;
swap(nums[i], nums[start]);
permute(nums, start + , res);
swap(nums[i], nums[start]);
}
}
};
同样,我们再测试下 [1, 2, 2] 这个例子,并且把中间变量打印出来:
start = , i = => { } , j = -
start = , i = => { } , j =
start = , i = => { } , j =
start = => saved { }
start = , i = => { } skipped, j =
start = , i = => { } -> { }, j = -
start = , i = => { } , j =
start = , i = => { } , j =
start = => saved { }
start = , i = => { } -> { }, j =
start = , i = => { } , j =
start = => saved { }
start = 1, i = 2 => {2 2 1} -> {2 1 2} recovered
start = 0, i = 1 => {2 1 2} -> {1 2 2} recovered
start = , i = => { } skipped, j =
到 start = 0, i = 2 的时候,j 此时等于1了,明显不是 start-1,说明有重复了,直接 skip 掉,这样剪枝操作就可以发挥作用了。
之前的 Permutations 中的解法三也可以用在这里,只不过需要借助 TreeSet 来去重复,博主还未想出其他不用集合的去重复的方法,哪位看官大神们知道的话,请一定要留言告知博主,参见代码如下:
解法五:
class Solution {
public:
vector<vector<int>> permuteUnique(vector<int>& nums) {
if (nums.empty()) return vector<vector<int>>(, vector<int>());
set<vector<int>> res;
int first = nums[];
nums.erase(nums.begin());
vector<vector<int>> words = permuteUnique(nums);
for (auto &a : words) {
for (int i = ; i <= a.size(); ++i) {
a.insert(a.begin() + i, first);
res.insert(a);
a.erase(a.begin() + i);
}
}
return vector<vector<int>> (res.begin(), res.end());
}
};
之前的 Permutations 中的解法四博主没法成功修改使其可以通过这道题,即便是将结果 res 用 TreeSet 来去重复,还是不对。同样,哪位看官大神们知道的话,请一定要留言告知博主。后经过微信公众号上的热心网友 hahaboy 的提醒下,可以通过加上一个剪枝从而通过这道题,在最中间的 for 循环的最后,判断若 num 等于 t[i],直接 break 掉当前循环,否则会产生重复项,参见代码如下:
解法六:
class Solution {
public:
vector<vector<int>> permuteUnique(vector<int>& nums) {
vector<vector<int>> res{{}};
for (int num : nums) {
for (int k = res.size(); k > ; --k) {
vector<int> t = res.front();
res.erase(res.begin());
for (int i = ; i <= t.size(); ++i) {
vector<int> one = t;
one.insert(one.begin() + i, num);
res.push_back(one);
if (i < t.size() && num == t[i]) break;
}
}
}
return res;
}
};
之前的 Permutations 中的解法五却可以原封不动的搬到这道题来,看来自带的 next_permutation() 函数就是叼啊,自带去重复功能,叼叼叼!参见代码如下:
解法七:
class Solution {
public:
vector<vector<int>> permuteUnique(vector<int>& nums) {
vector<vector<int>> res;
sort(nums.begin(), nums.end());
res.push_back(nums);
while (next_permutation(nums.begin(), nums.end())) {
res.push_back(nums);
}
return res;
}
};
Github 同步地址:
https://github.com/grandyang/leetcode/issues/47
类似题目:
参考资料:
https://leetcode.com/problems/permutations-ii/
https://leetcode.com/problems/permutations-ii/discuss/18601/Short-iterative-Java-solution
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] Permutations II 全排列之二的更多相关文章
- [LeetCode] 47. Permutations II 全排列之二
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- [Leetcode] permutations ii 全排列
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- leetcode Permutations II 无重全排列
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4051169.html 题目链接:leetcode Permutations II 无重全排 ...
- LeetCode: Permutations II 解题报告
Permutations II Given a collection of numbers that might contain duplicates, return all possible uni ...
- [LeetCode] 47. Permutations II 全排列 II
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- LeetCode Permutations II (全排列)
题意: 给出n个元素(可能有重复的),请产生出所有的全排列. 思路: 同版本1的有点不同,这次有可能含有重复的元素,很容易就TLE,节省时间才是关键点. 如果将一个序列中两个相同的元素交换,这个序列是 ...
- [leetcode]Permutations II @ Python
原题地址:https://oj.leetcode.com/problems/permutations-ii/ 题意: Given a collection of numbers that might ...
- leetcode -- Permutations II TODO
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- [Leetcode] Permutations II
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
随机推荐
- AngularJs学习笔记(制作留言板)
原文地址:http://www.jmingzi.cn/?post=13 初学Anjularjs两天了,一边学一边写的留言板,只有一级回复嵌套.演示地址 这里总结一下学习的过程和笔记.另外,看看这篇文章 ...
- 打造高效前端工作环境-tmuxinator
前言 虽然tmux能让我们方便组织工作环境,但每次重新打开会话时都需要手动重新创建窗口.窗格和执行各种程序,能不能像VS那样以工程为单位保存窗口.窗格和各种所需执行的程序的信息呢?tmuxinato ...
- OData Client Code Generator
转发. [Tutorial & Sample] How to use OData Client Code Generator to generate client-side proxy cla ...
- Java接口响应超时监控
为什么要监控 服务化接口是提供服务的,接口正确性.稳定性是最最重要的,在保证正确的同时需要尽量提高接口响应时间. 有的团队会有专门的工具来对系统响应时间.吞吐量做监控,但如果团队没有这种"待 ...
- 在Eclipse中使用Git
一.打开Eclipse,以此点击菜单Help--Install New Software-, 此时将弹出Install对话框,如下图所示: 点击Add按钮,此时将弹出Add Repository对话框 ...
- 9.1.3 .net framework通过业务逻辑层自动生成WebApi的做法
首先需要说明的是这是.net framework的一个组件,而不是针对.net core的.目前工作比较忙,因此.net core的转换正在编写过程中,有了实现会第一时间贴出来. 接下来进入正题.对于 ...
- 解决springmvc+mybatis+mysql中文乱码问题【转】
这篇文章主要介绍了解决java中springmvc+mybatis+mysql中文乱码问题的相关资料,需要的朋友可以参考下 近日使用ajax请求springmvc后台查询mysql数据库,页面显示中文 ...
- node使用xml-writer生成本地XML文件实例
npm中xml-writer文档的链接地址:https://www.npmjs.com/package/xml-writer npm中的文档比较简单,而且生成本地xml文件的demo并不正确.本篇是对 ...
- 【干货分享】前端面试知识点锦集04(Others篇)——附答案
四.Others部分 技术类 1.http状态码有哪些?分别代表是什么意思? (1).成功2×× 成功处理了请求的状态码.200 服务器已成功处理了请求并提供了请求的网页.204 服务器成功处理了请求 ...
- 20款 JavaScript 开发框架推荐给前端开发者
下面,我们给大家提供了一个用于 HTML5 开发的各种用途的 JavaScript 库列表.这些框架能够给前端开发人员提供更好的功能实现的解决方案.如果你有收藏优秀的框架,也可以在后面的评论中分享给我 ...