47. Permutations II
来源:互联网 发布:解西游知乎 编辑:程序博客网 时间:2024/05/18 02:01
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]
, and [2,1,1]
.
Solution 1 Use set
public List<List<Integer>> permuteUnique(int[] num) {Set<List<Integer>> permutations = new HashSet<List<Integer>>();if (num.length > 0) {permutations.add(Arrays.asList(num[0]));for (int index = 1; index < num.length; index++) {Set<List<Integer>> newPermutations = new HashSet<List<Integer>>();for (List<Integer> list : permutations) {for (int innerIndex = 0; innerIndex <= list.size(); innerIndex++) {List<Integer> newList = new ArrayList(list);newList.add(innerIndex, num[index]);newPermutations.add(newList);}}permutations = newPermutations;}}return new ArrayList<List<Integer>>(permutations);}Solution 2
public static List<List<Integer>> permuteUnique2(int[] nums) {List<List<Integer>> res = new ArrayList<List<Integer>>();Arrays.sort(nums);LinkedList<Integer> list = new LinkedList<Integer>();for (int num : nums)list.add(num);permute2(list, 0, res);return res;}private static void permute2(LinkedList<Integer> nums, int start, List<List<Integer>> res) {if (start == nums.size() - 1) {res.add(new LinkedList<Integer>(nums));return;}for (int i = start; i < nums.size(); i++) {if (i > start && nums.get(i) == nums.get(i - 1))continue;nums.add(start, nums.get(i));// swapnums.remove(i + 1);permute2(nums, start + 1, res);nums.add(i + 1, nums.get(start));nums.remove(start);}}Solution 3 Same idea with the last one
/* * The idea is for each recursion level, swap the current element at 1st * index with each element that comes after it (including itself) */// https://leetcode.com/discuss/84305/share-my-java-code-with-detailed-explanantionpublic List<List<Integer>> permuteUnique3(int[] nums) {List<List<Integer>> ans = new ArrayList<>();if (nums == null || nums.length == 0) {return ans;}permute(ans, nums, 0);return ans;}private void permute(List<List<Integer>> ans, int[] nums, int index) {if (index == nums.length - 1) {List<Integer> temp = new ArrayList<>();for (int num : nums) {temp.add(num);}ans.add(temp);return;}Set<Integer> appeared = new HashSet<>();for (int i = index; i < nums.length; ++i) {if (appeared.add(nums[i])) {swap(nums, index, i);permute(ans, nums, index + 1);swap(nums, index, i);}}}private void swap(int[] nums, int i, int j) {int save = nums[i];nums[i] = nums[j];nums[j] = save;}
0 0
- 46. Permutations && 47. Permutations II
- 46. Permutations, 47. Permutations II
- [LeetCode]47.Permutations II
- LeetCode 47.Permutations II
- LeetCode --- 47. Permutations II
- [Leetcode] 47. Permutations II
- [leetcode] 47.Permutations II
- 47.Permutations II
- 47. Permutations II
- LeetCode 47. Permutations II
- 47. Permutations II
- 47. Permutations II LeetCode
- 47. Permutations II
- 47. Permutations II
- 47. Permutations II
- 47. Permutations II
- Leetcode 47. Permutations II
- 47. Permutations II
- sql语句备忘
- 用Spotlight实时监控Windows Server 08
- ArrayList和数组的相互转换
- CSS创建图片库&透明图片&媒介类型
- (译)OpenGL ES2.0 – Iphone开发指引------------非常不错,关于opengl ls的说明
- 47. Permutations II
- 仿华为系统自带天气预报UI---线条圆圈控件
- [原创翻译]《Netty in Action》英文2016年版中文翻译版·第一章:异步和事件驱动
- angular学习笔记
- JS中使用动态原型模式、寄生构造函数模式、稳妥构造函数模式创建对象
- 获取随机打乱的ArrayList
- java中的自动类型转换和强制类型转换
- 算法分析之——heap-sort堆排序
- Hibernate单向多对一关联