给出数组,数组中的元素可能有重复,求出所有的全排列
 
使用递归算法:
 
传递参数 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. 干货分享 超炫丽的HTML5/jQuery应用及代码

    今天我们要来分享一些HTML5干货,对于前段开发者来说,这些HTML5应用和jQuery插件将会让你的项目更加生动和炫丽,至少在UI设计方面,或多或少会带给你一点设计灵感,赶紧收藏和分享吧.另外这些H ...

  2. TensorFlow-GPU:查看电脑显卡型号是否支持CUDN,以及相关软件下载与介绍

    1.显卡(GPU)是否支持CUDN https://developer.nvidia.com/cuda-gpus 2.了解基础知识 1)CUDA(Compute Unified Device Arch ...

  3. Microsoft VBScript 运行时错误 错误 '800a0046' 没有权限 解决方法

    首先看看是不是scrrun.dll 设置了拒绝权限(在windows/system32目录,右键文件,添加 IUSR+机器名 账户).... 一.如果您的系统提示"没有找到scrrun.dl ...

  4. [转]java线程安全、jstack\线程dump、内存查看分析总结

    http://jameswxx.iteye.com/blog/808546 java线程安全总结二 http://jameswxx.iteye.com/blog/1041173 jstack和线程du ...

  5. 【LSTM】Understanding-LSTMs

    http://colah.github.io/posts/2015-08-Understanding-LSTMs/

  6. Android 虚拟键盘弹出把底部栏顶上去的解决办法

    在AndroidManifest中使用ActivityGroup的activity中加上:android:windowSoftInputMode="adjustPan"

  7. 教你ABBYY FineReader 12添加图像的技巧

    ABBYY FineReader 12是一款OCR图片文字识别软件,而且强大的它现在还可使用快速扫描窗口中的快速打开.扫描并保存为图像或任务自动化任务,在没有进行预处理和OCR的ABBYY FineR ...

  8. Activiti 5.1.4最佳实践

    1.简单介绍 Activiti是一个开源的工作流引擎,它实现了BPMN 2.0规范,可以发布设计好的流程定义,并通过api进行流程调度. Activiti 作为一个遵从 Apache 许可的工作流和业 ...

  9. SpringMVC由浅入深day01_3非注解的处理器映射器和适配器

     3 非注解的处理器映射器和适配器 3.1 非注解的处理器映射器 3.1.1 HandlerMapping处理器映射器 HandlerMapping 负责根据request请求找到对应的Handler ...

  10. Jquery Ajax 和json用法

    向您的页面添加 jQuery 库 jQuery 库位于一个 JavaScript 文件中,其中包含了所有的 jQuery 函数. 可以通过下面的标记把 jQuery 添加到网页中: <head& ...