[LeetCode][Java] Permutations II
2015-07-14 11:56
417 查看
题目:
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].
题意:
给定一个整数集合,这个集合可能包含重复,返回所有的存在且唯一的组合。比如:
[1,1,2]拥有下面的这些组合:
[1,1,2],
[1,2,1],
and
[2,1,1].
算法分析:
方法一:这个题跟题目《Permutations》非常类似,唯一的区别就是在这个题目中元素集合可以出现重复。在上一个算法的基础上,当我们枚举第i个位置的元素时,若要把后面第j个元素和i交换,则先要保证[i…j-1]范围内没有和位置j相同的元素。可行的做法是:可以每次在需要交换时进行顺序查找。
方法二:
参考博客http://blog.csdn.net/linhuanmars/article/details/21570835
这个题跟Permutations非常类似,唯一的区别就是在这个题目中元素集合可以出现重复。这给我们带来一个问题就是如果不对重复元素加以区别,那么类似于{1,1,2}这样的例子我们会有重复结果出现。那么如何避免这种重复呢?方法就是对于重复的元素循环时跳过递归函数的调用,只对第一个未被使用的进行递归,我们那么这一次结果会出现在第一个的递归函数结果中,而后面重复的会被略过。如果第一个重复元素前面的元素还没在当前结果中,那么我们不需要进行递归。想明白了这一点,代码其实很好修改。首先我们要对元素集合排序,从而让重复元素相邻,接下来就是一行代码对于重复元素和前面元素使用情况的判断即可。
AC代码:
public class Solution { public ArrayList<ArrayList<Integer>> permuteUnique(int[] num) { ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>(); permuteUnique(num, 0, result); return result; } private static void permuteUnique(int[] num, int start, ArrayList<ArrayList<Integer>> result) { if (start >= num.length ) { ArrayList<Integer> item = convertArrayToList(num); result.add(item); } for (int j = start; j <= num.length-1; j++) { if (containsDuplicate(num, start, j)) { swap(num, start, j); permuteUnique(num, start + 1, result); swap(num, start, j); } } } private static void swap(int[] a, int i, int j) { int temp = a[i]; a[i] = a[j]; a[j] = temp; } private static ArrayList<Integer> convertArrayToList(int[] num) { ArrayList<Integer> item = new ArrayList<Integer>(); for (int h = 0; h < num.length; h++) item.add(num[h]); return item; } private static boolean containsDuplicate(int[] arr, int start, int end) { for (int i = start; i <= end-1; i++) { if (arr[i] == arr[end]) return false; } return true; } }
方法二:
public ArrayList<ArrayList<Integer>> permuteUnique(int[] num) { ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>(); if(num==null && num.length==0) return res; Arrays.sort(num); helper(num, new boolean[num.length], new ArrayList<Integer>(), res); return res; } private void helper(int[] num, boolean[] used, ArrayList<Integer> item, ArrayList<ArrayList<Integer>> res) { if(item.size() == num.length) { res.add(new ArrayList<Integer>(item)); return; } for(int i=0;i<num.length;i++) { if(i>0 && !used[i-1] && num[i]==num[i-1]) continue; if(!used[i]) { used[i] = true; item.add(num[i]); helper(num, used, item, res); item.remove(item.size()-1); used[i] = false; } } }
相关文章推荐
- JAVA二分递归查找算法
- java 基础 - 变量的修饰 - 顺序
- spring 基础知识复习
- 我的第一堂Java课 07/14 上午
- 我的第一堂Java课 07/14 上午
- Eclipse---Android 软件开发之如何使用Eclipse Debug调试程序详解
- Struts2实现文件上传和下载
- SpringMvc 两种上传。
- java事件机制
- 集合总览--Collection框架整体解析
- Spring源码解析 依赖注入
- 《Spring技术内幕》学习笔记5——IoC容器的依赖注入
- 自己封装的java db可以和spring 进行整合
- java读取判断文件编码格式
- Spring MVC 搭建
- 学习Java Web开发【2】可传输图片的服务器的代码编写
- Eclipse 中同一个类内 选中一个变量,在类内其他地方的 该变量高亮标识出来的设置方法
- Java中如何遍历Map对象的4种方法
- [LeetCode][Java] Permutations
- Java设计模式----工厂模式(一)