给出数组,数组中的元素可能有重复,求出所有的全排列
 
使用递归算法:
 
传递参数 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. linux下时间同步的两种方法分享

    方法1:与一个已知的时间服务器同步 复制代码 代码如下: ntpdate time.nist.gov 其中 time.nist.gov 是一个时间服务器. 删除本地时间并设置时区为上海 复制代码 代码 ...

  2. 每天一个linux命令:wc命令

    Linux系统中的wc(Word Count)命令的功能为统计指定文件中的字节数.字数.行数,并将统计结果显示输出. 1.命令格式: wc [选项]文件... 2.命令功能: 统计指定文件中的字节数. ...

  3. 如何解析本地和线上XML文件获取相应的内容

    一.使用Dom解析本地XML 1.本地XML文件为:test.xml <?xml version="1.0" encoding="UTF-8"?> ...

  4. 输出到网页前台js代码中包含单引号的处理方法

    描述:后台输出js到前台,如     <script type="text/javascript">   //<![CDATA[       var aStepD ...

  5. GetDlgItem的用法小结

    GetDlgItem用于获得指定控件ID的窗体指针,函数原型如下: HWND GetDlgItem( HWND hDlg, int nIDDlgItem ); CWnd* GetDlgItem(int ...

  6. EasyUI的功能树之异步树

    最近几个项目都用到了EasyUI这个Jquery框架,目前感觉起来还是很好使的,展示效果很好,帮助文档什么的资料很多,而且互联网上Easy粉很多,大多数拥护和喜爱EasyUI的粉丝们都愿意在网络平台互 ...

  7. mysql中如何查看某个日期所在的周一是几号?某个日期所在的一周开始时间是几号?

    需求描述: 在编写SQL的时候,有这么个需求,就是要查出来某个日期所在的周一是几号,进行了测试,在此进行记录下. 测试过程: 议题:查看某个日期所在的周一是几号 分析:如果某个日期是周一,那么加上整数 ...

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

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

  9. flume使用之exec source收集各端数据汇总到另外一台服务器

    转载:http://blog.csdn.net/liuxiao723846/article/details/78133375 一.场景一描述: 线上api接口服务通过log4j往本地磁盘上打印日志,在 ...

  10. MTK 隐藏上方的状态栏

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