Permutations II 



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



For example,

[1,1,2] have the following unique permutations:

[1,1,2], [1,2,1], and [2,1,1].

思路:这题相比于上一题,是去除了反复项。

代码上与上题略有区别。详细代码例如以下:

public class Solution {
boolean[] b;
List<List<Integer>> list;
Set<String> al;
public List<List<Integer>> permuteUnique(int[] nums) {
b = new boolean[nums.length];
Arrays.fill(b,true);
Arrays.sort(nums);
list = new ArrayList<List<Integer>>();
al = new HashSet<String>();
count(nums, "", nums.length);
//对al数据进行处理
Iterator<String> iterator = al.iterator();
//迭代器
while(iterator.hasNext()){
String s = iterator.next();
List<Integer> newal = new ArrayList<Integer>();
for(int i = 0; i < s.length();i++){
if(s.charAt(i) == '-'){//有负号
newal.add('0' - s.charAt(++i) );
}else{//无负号
newal.add(s.charAt(i) - '0');
}
}
list.add(newal);
}
return list;
}
/**
* @param nums 要排列的数组
* @param str 已经排列好的字符串
* @param nn 剩下须要排列的个数,假设须要全排列,则nn为数组长度
*/
void count(int[] nums,String str,int nn){
if(nn == 0){
al.add(str);//先加入到al中,再对al数据进行处理
return;
}
for(int i = 0; i < nums.length; i++){
if(nn == nums.length && i > 0 && nums[i] == nums[i-1]){
continue;//去除反复项
}
if(b[i]){//假设还没有组合,则组合上
b[i] = false;//标记为已组合
count(nums,str + nums[i],nn-1);
b[i] = true;//为下一组合准备
}
}
}
}

可是上述代码在数据量比較大的时候效率非常低,如数据有10个数据时大概耗时200ms。在论坛上看到了一个大神的代码。10个数据的耗时约25ms,效率非常高。

详细代码例如以下:

public class Solution {
public List<List<Integer>> permuteUnique(int[] num) {
List<List<Integer>> returnList = new ArrayList<List<Integer>>();
returnList.add(new ArrayList<Integer>()); for (int i = 0; i < num.length; i++) {
Set<List<Integer>> currentSet = new HashSet<List<Integer>>();
for (List<Integer> l : returnList) {
for (int j = 0; j < l.size() + 1; j++) {
l.add(j, num[i]);
List<Integer> T = new ArrayList<Integer>(l);
l.remove(j);
currentSet.add(T);
}
}
returnList = new ArrayList<List<Integer>>(currentSet);
} return returnList;
}
}

leetCode 47.Permutations II (排列组合II) 解题思路和方法的更多相关文章

  1. leetCode 60.Permutation Sequence (排列序列) 解题思路和方法

    The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

  2. leetCode 34.Search for a Range (搜索范围) 解题思路和方法

    Search for a Range Given a sorted array of integers, find the starting and ending position of a give ...

  3. leetCode 64.Minimum Path Sum (最短路) 解题思路和方法

    Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...

  4. leetCode 20.Valid Parentheses (有效的括号) 解题思路和方法

    Valid Parentheses  Given a string containing just the characters '(', ')', '{', '}', '[' and ']', de ...

  5. leetCode 36.Valid Sudoku(有效的数独) 解题思路和方法

    Valid Sudoku Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku bo ...

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

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

  7. leetCode 95.Unique Binary Search Trees II (唯一二叉搜索树) 解题思路和方法

    Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...

  8. leetCode 90.Subsets II(子集II) 解题思路和方法

    Given a collection of integers that might contain duplicates, nums, return all possible subsets. Not ...

  9. [leetcode] 题型整理之排列组合

    一般用dfs来做 最简单的一种: 17. Letter Combinations of a Phone Number Given a digit string, return all possible ...

随机推荐

  1. URAL1960 Palindromes and Super Abilities

    After solving seven problems on Timus Online Judge with a word “palindrome” in the problem name, Mis ...

  2. 【Codeforces Round #518 (Div. 2)】

    A:https://www.cnblogs.com/myx12345/p/9847588.html B:https://www.cnblogs.com/myx12345/p/9847590.html ...

  3. ubuntu php编译安装配置

    安装参考:http://ilanni.blog.51cto.com/526870/1569322/ 加载扩展的一些参数参考:http://java-er.com/blog/nginx-php-fpm/

  4. 调用Outlook发送邮件

    #region 查找与指定文件关联在一起的程序的文件名 /// <summary> /// 查找与指定文件关联在一起的程序的文件名 /// </summary> /// < ...

  5. [LeetCode] Gas Station 贪心

    There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You ...

  6. android中提示&对话框----Notification

    Notification(状态栏通知) 一.Notification用于状态栏显示通知的控件,在不同的设备上面Notification是不一样的 二.Notification的基本布局 元素组成: I ...

  7. hdu 1005(找循环节)

    Number Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  8. LeetCode OJ-- Search a 2D Matrix

    https://oj.leetcode.com/problems/search-a-2d-matrix/ 具有数据递增性质的一个二维数组,对它进行二分搜索. 首先搜索target所在的行,再找列. c ...

  9. NWU现场赛——解题报告

    负二进制转换 Time Limit : 3000/1000ms (Java/Other)   Memory Limit : 65535/32768K (Java/Other) Problem Desc ...

  10. bzoj 2889: Tree Conundrum

    2889: Tree Conundrum Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 61  Solved: 37[Submit][Status][ ...