leetCode 47.Permutations II (排列组合II) 解题思路和方法
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) 解题思路和方法的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- leetCode 20.Valid Parentheses (有效的括号) 解题思路和方法
Valid Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', de ...
- leetCode 36.Valid Sudoku(有效的数独) 解题思路和方法
Valid Sudoku Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku bo ...
- [LeetCode] 47. Permutations II 全排列 II
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- 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 ...
- leetCode 90.Subsets II(子集II) 解题思路和方法
Given a collection of integers that might contain duplicates, nums, return all possible subsets. Not ...
- [leetcode] 题型整理之排列组合
一般用dfs来做 最简单的一种: 17. Letter Combinations of a Phone Number Given a digit string, return all possible ...
随机推荐
- Codeforces 297C. Splitting the Uniqueness
C. Splitting the Uniqueness time limit per test:1 second memory limit per test:256 megabytes input:s ...
- sql语句中的[]中括号(转)
下面有关sql语句中[]的信息适用于Sql Server.Access等多种数据库. 1.sql语句中加[]是为了防止歧义,避免与系统保留关键字冲突,使计算机能识别.sql语句中有些字段可能是关键字, ...
- Codeforces 629 B. Far Relative’s Problem
B. Far Relative’s Problem time limit per test 2 seconds memory limit per test 256 megabytes inpu ...
- [Machine Learning with Python] Data Preparation through Transformation Pipeline
In the former article "Data Preparation by Pandas and Scikit-Learn", we discussed about a ...
- 透过ReentrantLock窥探AQS
背景 JDK1.5引入的并发包提供了一系列支持中等并发的类,这些组件是一系列的同步器,几乎任一同步器都可以实现其他形式的同步器,例如,可以用可重入锁实现信号量或者用信号量实现可重入锁.但是,这样做带来 ...
- 一次程序bug的排查
这周准备下一个QA测试的版本,把版本发到测试环境就开始发现各种问题,修修补补搞了一周,总算告一段落了. 分析一下几个bug的问题,都集中在程序模块的整合中.一个模块的一个小的修改,造成另一个模块的 ...
- Delphi通过管道执行外部命令行程序(cmd)并获取返回结果
该代码片段来自于: http://www.sharejs.com/codes/delphi/8999,发现好多代码,想用的时候找不到,记录一下备用 function RunDosCommand(Com ...
- ORACLE普通表转换成分区表
转http://mp.weixin.qq.com/s?__biz=MzAwMjkyMjEwNg==&mid=2247484761&idx=1&sn=ce080581145931 ...
- Netty4.0 用户指南
原文链接http://netty.io/wiki/user-guide-for-4.x.html 前言 Nowadays we use general purpose applications or ...
- AngularJS的稍复杂form验证
代码下载:https://files.cnblogs.com/files/xiandedanteng/angularjsSoccerFormCheck.rar 代码: <!DOCTYPE HTM ...