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 ...
随机推荐
- 水晶报表 IE设置
水晶报表:Crystal Reports(水晶报表)是一款商务智能(BI)软件,主要用于设计及产生报表.水晶报表是业内最专业.功能最强的报表系统,它除了强大的报表功能外,最大的优势是实现了与绝大多数流 ...
- MySQL冗余数据的三种方案
一,为什么要冗余数据 互联网数据量很大的业务场景,往往数据库需要进行水平切分来降低单库数据量. 水平切分会有一个patition key,通过patition key的查询能够直接定位到库,但是非pa ...
- C#将图片进行马赛克处理
/// <summary> /// 马赛克处理 /// </summary> /// <param name="bitmap"></par ...
- 【linux】进程存储管理
看<Linux高级程序设计>的笔记 设有一个hello的可执行文件 ①显示该文件的基本信息 ls hello -l ②文件基本情况 file hello ③列出文件的存储区域情况 size ...
- hdu 4528(搜索好题)
小明系列故事——捉迷藏 Time Limit: 500/200 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total S ...
- NanoPC-T3 64位裸机编程 —— 启动和运行状态切换
参考: https://github.com/metro94/s5p6818_spl https://github.com/trebisky/Fire3/tree/master/Boot_NSIH h ...
- 洛谷——P2205 [USACO13JAN]画栅栏Painting the Fence
题目描述 Farmer John has devised a brilliant method to paint the long fence next to his barn (think of t ...
- 2016集训测试赛(二十四)Problem C: 棋盘控制
Solution 场上的想法(显然是错的)是这样的: 我们假设棋子是一个一个地放置的, 考虑在放置棋子的过程中可能出现哪些状态. 我们令有序整数对\((i, j)\)表示总共控制了\(i\)行\(j\ ...
- fs寄存器相关,PEB,TEB
---恢复内容开始--- FS寄存器指向:偏移 说明000 指向SEH链指针004 线程堆栈顶部008 线程堆栈底部00C SubSystemTib010 FiberData014 Arbitrary ...
- ZFS -世界上最高级的文件系统之一
https://www.oschina.net/news/44302/openzfs_launch_announcement https://en.wikipedia.org/wiki/ZFS ZFS ...