leetcode-47. Permutations II(重复元素全排列)
2017-05-19 21:59
453 查看
https://leetcode.com/problems/permutations-ii/#/description
问题描述:
含有重复元素的全排列
思路解析:
参考http://blog.csdn.net/u013275928/article/details/72510351
这次考虑了重复元素,新增了一个用来表示该元素是否已经加入到了tempList中的boolean数组,若为真则已经存在,否则加入到templist中。另外,还需要考虑一点,由于先对数组进行排序,相同的值相邻,如果前面的值未加入到templist中,不可以先加入后面的值。
代码如下:
问题描述:
含有重复元素的全排列
思路解析:
参考http://blog.csdn.net/u013275928/article/details/72510351
这次考虑了重复元素,新增了一个用来表示该元素是否已经加入到了tempList中的boolean数组,若为真则已经存在,否则加入到templist中。另外,还需要考虑一点,由于先对数组进行排序,相同的值相邻,如果前面的值未加入到templist中,不可以先加入后面的值。
代码如下:
public class Solution { public List<List<Integer>> permuteUnique(int[] nums) { Arrays.sort(nums); List<List<Integer>> result=new ArrayList<>(); boolean[] used=new boolean[nums.length]; getDupPer(result,new ArrayList<>(),nums,used); return result; } public void getDupPer(List<List<Integer>> result,List<Integer> temp,int[] nums,boolean[] used) { if(temp.size()==nums.length) { result.add(new ArrayList<>(temp)); }else { for(int i=0;i<nums.length;i++) { if( used[i] )continue; if (i>0 && nums[i-1]==nums[i] && !used[i-1]) continue; used[i]=true; temp.add(nums[i]); getDupPer(result,temp,nums,used); used[i]=false; temp.remove(temp.size()-1); } } } }
相关文章推荐
- leetcode 47. Permutations II 全排列问题(去掉重复元素)+递归
- leetcode - 47. Permutations II(全排列)
- 47. Permutations II (全排列有重复的元素)
- LeetCode-----46.Permutations&&47.Permutations II (全排列----回溯法)
- leetcode-46. Permutations(非重复元素全排列)
- LeetCode 47 Permutations II(全排列)
- LeetCode 从排序好的列表中删除重复元素
- 含重复元素序列的全排列
- [Leetcode 101] 47 Permutations II
- 【LeetCode题目记录-10】已排序的数组去除重复元素
- 全排列的java实现(无重复元素)
- 单词的全排列同时去重复的元素
- 含重复元素的全排列
- [LeetCode系列]子集枚举问题[无重复元素]
- [LeetCode系列]子集枚举问题[有重复元素]
- leetcode 题解:Remove Duplicates from Sorted Array II(已排序数组去三次及以上重复元素)
- Leetcode 最小二叉树深度和删除数组重复元素
- 如何得到有重复元素的不重复全排列
- leetcode:Remove Duplicates from Sorted List(去除链表中重复元素)【面试算法题】
- [LeetCode]—Permutations II 求全排列(有重复值)