permutations II(全排列 2)
题目要求
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]
]
对于其基础题PermutationsI请参考我的另一篇博客
这里添加的难度在于,排列组合的数字中可能存在重复。这就需要想方法,将结果集中重复的结果删去。而这里,我参考了另一名答题者的答案,在试图将数字添入结果集中时,就判断会不会产生重复的结果,从而使避免重复。
PermutationsI也可以使用此方法。。
代码和解析
这里采用了递归的思路。避免重复的核心思路在于,使用一个boolean数组来代表当前的数值是否已经被使用过。当前的值如果已经被使用过,则继续判断下一个数值。如果当前的值为重复值,则只要前面的值没有被使用过,则当前值就不可以被使用。这样确保了只有第一个出现的重复值可以算进结果集,后序重复的情况不会被添加进结果集。
例如,假设输入的数组为[1,1,2]。则当第一个1被添加进结果集时,可以继续使用第二个1作为元素添加进结果集从而生成112。同理,当试图将第二个1作为第一个元素添加进结果集时,只要第一个1还没有被使用过,则不可以使用第二个1。因此,112不会被重复的添加进结果集。
其实,这个算法保证了所有重复的数字在结果集中的顺序和在原输入数组中的顺序是相同的。假设将[1,1,2]表示为[1,1#,2],那么结果集中会确保1永远在数值1#的前面,从而避免了11#2和1#12的重复情况出现。
代码如下:
class Solution {
public List<List<Integer>> permuteUnique(int[] nums) {
List<List<Integer>> res=new ArrayList<List<Integer>>();
if(nums==null||nums.length==0) return res;
//该数组用来标记每个位置的元素是否使用以保证每个位置的元素只能使用一次。控制递归遍历(往list后面添加元素)中该元素是否已经使用。
boolean[] used=new boolean[nums.length];
Arrays.sort(nums);
List<Integer> list=new ArrayList<>();
helper(nums,res,list,used);
return res;
}
public void helper(int[] nums,List<List<Integer>> res,List<Integer> list,boolean[] used){
if(nums.length==list.size()){
res.add(new ArrayList<Integer>(list));
return ;
}
for(int i=0;i<nums.length;i++){
if(used[i]) continue; //该元素使用过了。
//下一个重复值只有在前一个重复值被使用的情况下才可以使用。
/*
这个!used[i-1]条件是为深度遍历准备的。
对于当前层遍历时,每遍历一个元素结束后就会设为false然后遍历下一个,所以可以跳过重复元素,避免相同元素出现在同一个位置。
对于深度遍历(递归,往结果集中继续添加元素),需要使用!used[i-1]来保证后面的相同元素可以往后面的结果集中添加。
比如[1,1,2].第一层遍历第一个1时,设为true后,递归往后面添加第二个元素,此时也是从第一个元素开始遍历,
因为此时used[0]为true,所以跳过,i=1,此时如果没有!used[i-1],也会跳过第二个1了,所以需要加上这个条件。
*/
if(i>0&&nums[i]==nums[i-1]&&!used[i-1]) continue;
used[i]=true;
list.add(nums[i]);
helper(nums,res,list,used);
//下面这两步,为了同层遍历,同层遍历是给一个位置轮流添加元素。所以需要将上一个添加的元素删了,然后删除标记,遍历下一个元素
used[i]=false;
list.remove(list.size()-1);
}
}
}
参考:https://segmentfault.com/a/1190000009513703
permutations II(全排列 2)的更多相关文章
- [LeetCode] Permutations II 全排列之二
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- [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] 47. Permutations II 全排列 II
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- 47. Permutations II (全排列有重复的元素)
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- Leetcode47. Permutations II全排列2
给定一个可包含重复数字的序列,返回所有不重复的全排列. 示例: 输入: [1,1,2] 输出: [ [1,1,2], [1,2,1], [2,1,1] ] 在全排列1题目的基础上先排序,目的是把相同的 ...
- leetcode Permutations II 无重全排列
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4051169.html 题目链接:leetcode Permutations II 无重全排 ...
- Leetcode之回溯法专题-47. 全排列 II(Permutations II)
Leetcode之回溯法专题-47. 全排列 II(Permutations II) 给定一个可包含重复数字的序列,返回所有不重复的全排列. 示例: 输入: [1,1,2] 输出: [ [1,1,2] ...
- LeetCode:Permutations, Permutations II(求全排列)
Permutations Given a collection of numbers, return all possible permutations. For example, [1,2,3] h ...
随机推荐
- 如何找到java对应的c/c++源码
很多时候java经常被c鄙视,因为c说我是你的基础,但java竟然有如此强的生命力就必然有其存在的价值.本文不探讨各种开发语言的优劣,仅仅介绍如何找到java对应c/c++实现的源码.当我们追究一个j ...
- Android 优质精准的用户行为统计和日志打捞方案
Android 自定义优质精准的用户行为和日志打捞方案 Tamic csdn博客 :http://blog.csdn.net/sk719887916/article/details/51398416 ...
- 择天记OL体验截图
- Guava 教程2-深入探索 Google Guava 库
原文出处: oschina 在这个系列的第一部分里,我简单的介绍了非常优秀的Google collections和Guava类库,并简要的解释了作为Java程序员,如果使用Guava库来减少项目中大量 ...
- 【Swift学习笔记00】——enumeration枚举类型遵循协议protocol
Apple官方文档:The Swift Programming LanguageProtocols and Extensions一节的小节练习,要求自行定义一个enumeration枚举类型,并且遵循 ...
- 纸飞机AE小教程
- Shell中read的常用方式
read命令的语法: read -p "Prompt" variable1 variable2 variableN -p "Prompt": 显示提示信息(和用 ...
- 简译《Dissecting SQL Server Execution Plans》——连载总入口
转载请注明出处 由于工作及学习需要,最近看了一下<Dissecting SQL Server Execution Plans>,这是少有的专门描述执行计划的优秀书籍,为了快速查找并供入门同 ...
- mysql进阶(七)limit的用法
limit是mysql的语法 select * from table limit m,n 其中m是指记录开始的index,从0开始,表示第一条记录 n是指从第m+1条开始,取n条. select * ...
- 【一天一道LeetCode】#40. Combination Sum II
一天一道LeetCode系列 (一)题目 Given a collection of candidate numbers (C) and a target number (T), find all u ...