给出数组,数组中的元素可能有重复,求出所有的全排列
 
使用递归算法:
 
传递参数 List<List<Integer>> list, List<Integer> tempList, int[] nums, boolean[] used
 
其中list保存最终结果
tempList保存其中一个全排列组合
nums保存初始的数组
used随着计算不断进行更新操作,记录所有数字是否已经使用
参考代码:
package leetcode_50;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; /***
*
* @author pengfei_zheng
* 给定数组元素可能重复,求出所有全排列
*/
public class Solution47 {
public static List<List<Integer>> permute(int[] nums) {
List<List<Integer>> ans = new ArrayList<List<Integer>>();
if(nums==null || nums.length==0) return ans;
boolean[] used = new boolean[nums.length];
List<Integer> list = new ArrayList<Integer>();
Arrays.sort(nums);
prem(ans,list,nums,used);
return ans;
} private static void prem(List<List<Integer>> list, List<Integer> tempList, int[] nums, boolean[] used) {
if(tempList.size()==nums.length){
list.add(new ArrayList<>(tempList));
}
else{
for(int i = 0; i < nums.length; i++){
if(used[i] || i>0 && nums[i]==nums[i-1] && !used[i-1]) continue;
used[i]=true;
tempList.add(nums[i]);
prem(list,tempList,nums,used);
used[i]=false;
tempList.remove(tempList.size()-1);
}
}
}
public static void main(String[]args){
int []nums={3,0,3,3};
List<List<Integer>> list = permute(nums);
for(List<Integer> item:list){
System.out.println(item);
}
}
}

LeetCode 47 Permutations II(全排列)的更多相关文章

  1. [LeetCode] 47. Permutations II 全排列 II

    Given a collection of numbers that might contain duplicates, return all possible unique permutations ...

  2. [LeetCode] 47. Permutations II 全排列之二

    Given a collection of numbers that might contain duplicates, return all possible unique permutations ...

  3. leetCode 47.Permutations II (排列组合II) 解题思路和方法

    Permutations II  Given a collection of numbers that might contain duplicates, return all possible un ...

  4. [leetcode] 47. Permutations II

    Given a collection of numbers that might contain duplicates, return all possible unique permutations ...

  5. 47. Permutations II (全排列有重复的元素)

    Given a collection of numbers that might contain duplicates, return all possible unique permutations ...

  6. [Leetcode][Python]47: Permutations II

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 47: Permutations IIhttps://oj.leetcode. ...

  7. 【LeetCode】47. Permutations II

    Permutations II Given a collection of numbers that might contain duplicates, return all possible uni ...

  8. Java for LeetCode 047 Permutations II

    Given a collection of numbers that might contain duplicates, return all possible unique permutations ...

  9. 【LeetCode】Permutations II 解题报告

    [题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...

随机推荐

  1. Mac mysql 解决中文乱码

    Mac mysql 解决中文乱码问题 出现"???"之类的无法识别的乱码 到/etc目录下自己建一个my.cnf文件(需要最高权限,使用sudo su),然后写入内容: [clie ...

  2. JAVA构造方法与方法是啥意思,方法重载方法覆盖俗谈

    构造函数跟构造方法是一样的,只是称呼不同; C语言里叫函数,Java里叫方法. 成员方法必须有返回类型即使是没有返回,也要写上void 构造方法没有返回类型,而且和类名一样!一个类里面,一看就知道了譬 ...

  3. UNIX环境编程学习笔记(22)——进程管理之system 函数执行命令行字符串

    lienhua342014-10-15 ISO C 定义了 system 函数,用于在程序中执行一个命令字符串.其声明如下, #include <stdlib.h> int system( ...

  4. How to make an HTTP request in Swift

    from: http://stackoverflow.com/questions/24016142/how-to-make-an-http-request-in-swift You can use N ...

  5. Uploading File using Ajax and receiving binary data in Asp.net (C#)[转]

    基础知识,可由此衍生.原文:http://uniapple.net/blog/?p=2050 In this post, I will show you how to upload a file us ...

  6. java 上传附件的一点积累

    1.第一种: File inFile = new File(downfileA);//downfileA是前台传过来的,文件路径String fileName = inFile.getName();S ...

  7. C语言的结构体

    举例,一个结构体的定义如下: typedef struct _foo { ]; int age; int sex; } foo; 对齐 如果直接对上面的结构体作sizeof()运算: printf( ...

  8. Git项目协同开发学习笔记1:项目库开发基础git命令

    这年头git基本都是项目开发的标配,之前刚好碰到了就花了两天时间系统学习了下.本文内容基本来自以下tutorial:Learn Git(建议直接去看原文,因为这个网站是有更新的).这个是我看过对git ...

  9. SpEL、PropertyPlaceholderConfigurer与@Value、#{}、${}

    概念 SpEL:Spring EL表达式 PropertyPlaceholderConfigurer:即org.springframework.beans.factory.config.Propert ...

  10. 【Access2007】将Excel表导入至Access2007的当中一张已存在的表之中

    将Excel表导入至Access2007,你会发现万恶的Access2007会帮你自己主动创建一张表.全然没有问你是否要插入一张已存在的表之中. 那么,我们须要这样解决: 一.依照正常的步骤先将Exc ...