LeetCode 47 Permutations II(全排列)
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(全排列)的更多相关文章
- [LeetCode] 47. Permutations II 全排列 II
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- [LeetCode] 47. Permutations II 全排列之二
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- leetCode 47.Permutations II (排列组合II) 解题思路和方法
Permutations II Given a collection of numbers that might contain duplicates, return all possible un ...
- [leetcode] 47. Permutations II
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- 47. Permutations II (全排列有重复的元素)
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- [Leetcode][Python]47: Permutations II
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 47: Permutations IIhttps://oj.leetcode. ...
- 【LeetCode】47. Permutations II
Permutations II Given a collection of numbers that might contain duplicates, return all possible uni ...
- Java for LeetCode 047 Permutations II
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- 【LeetCode】Permutations II 解题报告
[题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...
随机推荐
- C# 校验Email(电子邮件)地址是否合法
用于校验给定的Email地址是否合法,只针对用于提供的Email地址的格式,不对其是否真实存在进行校验. /// <summary> /// 验证EMail是否合法 /// </su ...
- Spock集成入门
本文基于SpringBoot 在pom.xml添加Spock依赖 <!-- test --> <dependency> <groupId>org.codehaus. ...
- ubuntu16.04 桌面图标左侧,右侧,底部进行切换
转载:https://jingyan.baidu.com/article/e52e36154e6af340c60c518c.html 传统的 Unity 桌面环境,其应用程序启动器的容器——Launc ...
- iOS:if a ViewController is Visible
from:stackoverflow The view's window property is non-nil if a view is currently visible, so check th ...
- LoadRunner javavuser错误排查
Loadrunner 9.5/11 使用java 开发vsuer script需要的环境配置 本文从两个方面来讲:windows 32位操作系统:windows 64 操作系统开始之前,先说下java ...
- 【Jupyter notebook】access remotly
http://jupyter-notebook.readthedocs.io/en/latest/public_server.html
- Java 连接 Access数据库方式
import <a href="http://lib.csdn.net/base/java" class='replace_word' title="Java 知识 ...
- level 6 - unit3 -- 非限制性定语从句
非限制性定语从句 例子1 he has a son who is a fireman who 引导一个定语从句. who 是修饰前面的son. 翻译的意思:他有一个消防员的儿子 he has a so ...
- android手机内的通讯录数据库
今天看了一下 android手机内的通讯录数据库,简单的汇总了一下. 数据库见附件中的contacts2.db , 里面一共有40个表,34个视图,很庞大,挑几个重点的看一下. 1.表Raw_cont ...
- 书籍记录——C++大学基础教程(第五版)
C++大学基础教程(第五版) Small C++ How to Program,Fifth Edition,H.M.Deitel,P.J.Deitel 第一章 计算机.互联网和万维网简介 第二章 C+ ...