给出数组,数组中的元素可能有重复,求出所有的全排列
 
使用递归算法:
 
传递参数 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. 7.1 安装软件包的三种方法 7.2 rpm包介绍 7.3 rpm工具用法 7.4 yum工具用法 7.5 yum搭建本地仓库

    7.1 安装软件包的三种方法 7.2 rpm包介绍 7.3 rpm工具用法 7.4 yum工具用法 7.5 yum搭建本地仓库 三种方法 rpm工具----->类型windows下的exe程序 ...

  2. Phpcms V9单页添加自定义字段

    说起文章自定义自段,大家都会想到 wordpress 的自定义字段,确实 wordpress 系统很强大,字段可以很灵活的在后台进行添加与更新,并能够很好的在前台进行调用,对于这点 phpcms v9 ...

  3. beego + websocket 向页面推送数据

    https://blog.csdn.net/u012210379/article/details/72901387 https://blog.csdn.net/u012210379/article/d ...

  4. jquery ajax 请求中 中文汉字的 转码问题

    1.汉字参数直接跟在请求连接的后面,这样需要使用encodeURIComponent(fileName)或者encodeURI(fileName)转码两次 后台使用URLDecoder.decode( ...

  5. MTK 隐藏上方的状态栏

    步骤一: 源码/frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBar.ja ...

  6. Linux命令缩写的全称

    [目录|文件] ls : list(列出目录内容) pwd : print work directory(打印当前目录,现示当前工作目录的绝对路径) cd : change directory(改变目 ...

  7. 使用npm国内镜像

    嫌npm指令速度慢的童鞋可以把npm的源转换成国内的即可提高响应速度: 镜像使用方法(三种办法任意一种都能解决问题,建议使用第1或者第3种,将配置写死,下次用的时候配置还在):1.通过config命令 ...

  8. Dubbo -- 系统学习 笔记 -- 示例 -- 静态服务

    Dubbo -- 系统学习 笔记 -- 目录 示例 想完整的运行起来,请参见:快速启动,这里只列出各种场景的配置方式 静态服务 有时候希望人工管理服务提供者的上线和下线,此时需将注册中心标识为非动态管 ...

  9. ios开发之--使用xib适配iPhone X

    最近在修改一个老项目,里面有很多xib文件,需要适配iPhone X,但是又不想重写页面用代码适配,分享个小方法,也算是个笨办法吧, 适配iPhone X底部,iPhone X底部有34px的操作区域 ...

  10. centos7修改root密码

    1.重启系统,在下面界面时按e键 2.出现可编辑新内容,按向下键向下滑动,找到ro,并修改为rw 后,在LANG=en_US.UTF-8后面再加init=/bin/sh,结果如下图 3.然后按下ctr ...