Description:

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]
] Thoughts:
这个问题和之前的permutations问题类似,区别在于这次给定的数字有重复的部分,但是我们输出的排列又不允许重复。排列的话,一个数字不允许重复使用,之前在permutions中我们用Nums.contains来规避这个问题,但是在现在这个问题中因为
nums中有重复的数字,所以这个方法不行了,解决的办法是我们开辟一个和nums等长的boolean数组addnums,addnums中值为true,表示nums中对应位置上的数字已经被包含在排列中,就要跳过这个数字。通过这个方法我们就解决了基本的排列问题。
还有一个问题需要解决的就是排列不允许重复,这个问题的解决方法就是跳过那些已经作为首数字出现过的数字。首先我们对于原始数组进行一次排序,然后在每次选择数字的时候判断,对于大于首字符的数字是否等于首字符,并且还没有使用过,
如果是的话就跳过。以下是我的java代码:
package middle;
import java.util.*;
public class PermutationTwo {
public List<List<Integer>> permuteUnique(int[] nums){
List<List<Integer>> result = new ArrayList<List<Integer>>(); Arrays.sort(nums);
ArrayList addnums = new ArrayList();
trackBack(nums, new ArrayList(), result, addnums);
return result;
} private void trackBack(int[] nums, ArrayList arrayList,
List<List<Integer>> result, ArrayList addnums) {
// TODO Auto-generated method stub
if(arrayList.size()==nums.length && result.contains(arrayList)==false){
result.add(new ArrayList(arrayList));
}
for(int i = 0; i<nums.length;i++){
// if(i > 0 && nums[i] == nums[i-1]) continue;
if(addnums.contains(i)) continue;
arrayList.add(nums[i]);
addnums.add(i);
trackBack(nums, arrayList, result, addnums);
arrayList.remove(arrayList.size() - 1);
addnums.remove(addnums.size() - 1);
}
} public List<List<Integer>> permuteUniqueTwo(int[] nums){
List<List<Integer>> result = new ArrayList<List<Integer>>(); Arrays.sort(nums);
trackBackTwo(nums, result, new ArrayList(), new boolean[nums.length]); return result;
} private void trackBackTwo(int[] nums, List<List<Integer>> result,
ArrayList arrayList, boolean[] use) {
// TODO Auto-generated method stub
if(arrayList.size() == nums.length){
result.add(new ArrayList(arrayList));
}else{
for(int i = 0; i< nums.length;i++){
if(use[i] || i > 0&&nums[i] == nums[i-1]&&use[i-1]==false) continue;
use[i] = true;
arrayList.add(nums[i]);
trackBackTwo(nums, result, arrayList, use);
arrayList.remove(arrayList.size() - 1);
use[i] = false;
}
}
} public static void main(String args[]){
PermutationTwo p = new PermutationTwo();
int[] nums = new int[]{1, -1, 1, 2, -1, 2, 2, -1};
List<List<Integer>> list = p.permuteUniqueTwo(nums);
System.out.println(list);
} }

还有第二种更加直观的解法,但是它的执行时间太慢了,leetcode上通过不了,但是为了更加便于理解我还是把它写出来。解决重复数字的问题,我是用一个ArrayList记录当前已经使用过的数字的位置,如果当前数字的位置在ArrayList中已经
出现过了,那就说明当前的数字已经用过了,就要跳过。然后我们根据得到的排列,判断当前的排列是否包含在result中,如果包含了,也就是说当前的排列已经出现过了,那么该排列就不能加入到result中。换句话说,只有新的排列才能够出现在result
中。以下是我的java代码:
package middle;
import java.util.*;
public class PermutationTwo {
public List<List<Integer>> permuteUnique(int[] nums){
List<List<Integer>> result = new ArrayList<List<Integer>>(); Arrays.sort(nums);
ArrayList addnums = new ArrayList();
trackBack(nums, new ArrayList(), result, addnums);
return result;
} private void trackBack(int[] nums, ArrayList arrayList,
List<List<Integer>> result, ArrayList addnums) {
// TODO Auto-generated method stub
if(arrayList.size()==nums.length && result.contains(arrayList)==false){
result.add(new ArrayList(arrayList));
}
for(int i = 0; i<nums.length;i++){
// if(i > 0 && nums[i] == nums[i-1]) continue;
if(addnums.contains(i)) continue;
arrayList.add(nums[i]);
addnums.add(i);
trackBack(nums, arrayList, result, addnums);
arrayList.remove(arrayList.size() - 1);
addnums.remove(addnums.size() - 1);
}
} public static void main(String args[]){
PermutationTwo p = new PermutationTwo();
int[] nums = new int[]{1, -1, 1, 2, -1, 2, 2, -1};
List<List<Integer>> list = p.permuteUnique(nums);
System.out.println(list);
} }

PermutationTwo的更多相关文章

随机推荐

  1. Java进阶(二)文件读操作

    本文以实际的读取文件为例子,介绍流的概念,以及输入流的基本使用. 按照前面介绍的知识,将文件中的数据读入程序,是将程序外部的数据传入程序中,应该使用输入流--InputStream或Reader.而由 ...

  2. Tomcat如何实现资源安全管理

    在了解了认证模式及Realm域后,我们看看Tomcat是如何设计实现资源安全管理的.在认证模式上,必须要支持多种认证模式,包括Basic模式.Digest模式.Form模式.Spnego模式.SSL模 ...

  3. 在IFrame中查找IFRAME中的元素的方式

    下面是内部iframe找外部mainFrame的情况  var websiteSearchButton = window.parent.parent.document.getElementById(' ...

  4. C语言实现万年历

    给出你想知道的年份,便可以计算出该年对应的每个月每个日所对应的星期数,是不是感觉很好玩 ? #include <stdio.h> #include<stdlib.h> long ...

  5. Linux进程模型

    ----原文链接:http://www.cnblogs.com/biyeymyhjob/archive/2012/08/01/2617884.html------ Linux进程通过一个task_st ...

  6. ECMAScript中所有的函数的参数都是按值传递的

    看下面一段代码 function setName(obj){ obj.name='Nicholas'; obj=new Object(); obj.name="Greg"; } v ...

  7. android驱动例子(LED灯控制)

    本例子,讲述在android2.1上完全自已开发一个驱动去控制硬件口并写应用测试该驱动,通过这样一个例子,解析android下的驱动开发流程的应用调用流程,可以说是很好的入门引导 要达到的效果:通过a ...

  8. 操作系统 - unix和windows下进程异同

    在UNIX系统中,只有一个系统调用可以用来创建新进程:fork.这个系统调用会创建一个与调用进程相同的副本.在调用了fork之后,这两个进程(父进程和子进程)拥有相同的存储映像.同样的环境字符串和同样 ...

  9. Struts源码之ValueStack

    /** * ValueStack allows multiple beans to be pushed in and dynamic EL expressions to be evaluated ag ...

  10. 速度之王 — LZ4压缩算法(二)

    LZ4 (Extremely Fast Compression algorithm) 项目:http://code.google.com/p/lz4/ 作者:Yann Collet 本文作者:zhan ...