Permutations II leetcode java
题目:
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].
题解:
这道题跟Permutaitons没啥大的区别,就是结果去重。
我之前也有写过去重的两个方法:
一个是在加入结果的时候用contains判断,一个是在找结果的时候看他是不是跟前一个元素相同。
这道题还要考虑的是visited情况,前一个元素就算跟当前元素相同,如果visited==true也没关系。但是如果前面元素跟当前元素相同还没被visited,那么就要做去重处理了。
代码如下:
1 public ArrayList<ArrayList<Integer>> permuteUnique(int[] num) {
2 ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>();
3 ArrayList<Integer> item = new ArrayList<Integer>();
4
5 if(num.length==0||num==null)
6 return res;
7 boolean[] visited = new boolean[num.length];
8 Arrays.sort(num);
9 permutation_helper(num,res,item,visited);
return res;
}
public void permutation_helper(int[] num, ArrayList<ArrayList<Integer>> res, ArrayList<Integer> item,boolean[] visited){
if(item.size()==num.length){
res.add(new ArrayList<Integer>(item));
return;
}
for(int i = 0; i<num.length;i++){
if(i>0 && num[i-1] == num[i] && !visited[i-1])
continue;
if(!visited[i]){
item.add(num[i]);
visited[i]=true;
permutation_helper(num,res,item,visited);
item.remove(item.size()-1);
visited[i]=false;
}
}
}
实验:
如果未加visited判断的话,将会出现下面的错误:
| Input: | [1,1] |
| Output: | [] |
| Expected: | [[1,1]] |
因为执行了去重处理,所以一个结果都没有保留
同时,这里在每次添加遍历的item时候,没有判断该元素是否之前被visited过,这样同样会产生重复。
另外一个错误是,for循环的起始是start,而非每次从0开始,这样的话,会忽略掉start位置之前,未visited过的,非重复值。
比如: [1, 2],第一次记录结果[1,2]是正常的没有问题。 但是当退栈到第一个栈,走for循环时,item位置的第一个元素是2, 进入下一层递归,发现start位置是1(0+1),1位置上面是元素2,2被visited过了,所以就结束了这个程序。那么位置在0的,值为1的元素,就被忽略掉了。因为start位置没有从0开始,所以每次都应该从0位置开始。
错误代码如下:
1 public ArrayList<ArrayList<Integer>> permuteUnique(int[] num) {
2 ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>();
3 ArrayList<Integer> item = new ArrayList<Integer>();
4
5 if(num == null || num.length == 0)
6 return res;
7 //boolean [] visited = new boolean[num.length];
8 Arrays.sort(num);
9 permutationhelper(res, num, item, 0);
return res;
}
public void permutationhelper(ArrayList<ArrayList<Integer>> res, int[] num, ArrayList<Integer> item, int start){
if(item.size() == num.length){
res.add(new ArrayList<Integer>(item));
return;
}
for(int i = start; i < num.length; i++){
if(i > 0 && num[i] == num[i-1])
continue;
item.add(num[i]);
permutationhelper(res, num, item, start+1);
item.remove(item.size()-1);
}
}
为了更清楚的知道整个程序是如何运行的,代码中把start标记标出,正确代码如下:
1 public ArrayList<ArrayList<Integer>> permuteUnique(int[] num) {
2 ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>();
3 ArrayList<Integer> item = new ArrayList<Integer>();
4
5 if(num == null || num.length == 0)
6 return res;
7 boolean [] visited = new boolean[num.length];
8 Arrays.sort(num);
9 permutationhelper(res, num, item, visited, 0);
return res;
}
public void permutationhelper(ArrayList<ArrayList<Integer>> res, int[] num, ArrayList<Integer> item, boolean[] visited, int start){
if(item.size() == num.length){
res.add(new ArrayList<Integer>(item));
return;
}
for(int i = start; i < num.length; i++){
if(i > 0 && num[i] == num[i-1] && !visited[i - 1])
continue;
if(!visited[i]){
item.add(num[i]);
visited[i] = true;
permutationhelper(res, num, item, visited, start);
visited[i] = false;
item.remove(item.size()-1);
}
}
}
Permutations II leetcode java的更多相关文章
- Permutations II - LeetCode
目录 题目链接 注意点 解法 小结 题目链接 Permutations II - LeetCode 注意点 不确定有几种排列 解法 解法一:因为有重复的数字所以排列的个数不确定几个,一直生成新的排列直 ...
- N-Queens II leetcode java
题目: Follow up for N-Queens problem. Now, instead outputting board configurations, return the total n ...
- Permutations II ——LeetCode
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- Single Number II leetcode java
问题描述: Given an array of integers, every element appears three times except for one. Find that single ...
- Word Break II leetcode java
题目: Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where e ...
- Palindrome Partitioning II Leetcode java
题目: Given a string s, partition s such that every substring of the partition is a palindrome. Return ...
- Remove Duplicates from Sorted List II leetcode java
题目: Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct ...
- Ugly Number II leetcode java
问题描述: Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime fa ...
- Word Ladder II leetcode java
题目: Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) ...
随机推荐
- 对字符串进行频繁拼接的话,使用StringBuffer或者StringBuilder
package zhengze; /*如果需要对字符串进行频繁拼接的话,使用StringBuffer或者StringBuilder StringBuffer:[字符串缓冲器]是线程安全的,效率低 St ...
- 解决在使用pip list时出现DEPRECATION
在C:\Users\Administrator下建立pip文件夹,在pip下新建pip.ini:内容为: [list]format=columns
- POJ 3280 Cheapest Palindrome【DP】
题意:对一个字符串进行插入删除等操作使其变成一个回文串,但是对于每个字符的操作消耗是不同的.求最小消耗. 思路: 我们定义dp [ i ] [ j ] 为区间 i 到 j 变成回文的最小代价.那么对于 ...
- vtiger二次开发
搞了快两个星期的vtiger,慢慢的摸索到了一些东西 数据库相当的复杂,已有的模块我只是分析了下页面的加载,方法的调用 大部分时间在研究怎么添加新的功能模块,今天才知道模块可以通过输入命令的方式来添加 ...
- 【莫比乌斯反演】HDU1695_GCD
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1695 第一道莫比乌斯反演 感觉很巧妙的就是利用了F(x)=(n/x)*(m/x) 之后的那个去重也挺不 ...
- 注册表键值明明存在OpenSubKey始终返回null,解决方案
先上代码及实例 RegistryKey rsg = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Macromedia\FlashPaper Pri ...
- Codeforces Round #395 (Div. 2)
今天自己模拟了一套题,只写出两道来,第三道时间到了过了几分钟才写出来,啊,太菜了. A. Taymyr is calling you 水题,问你在z范围内 两个序列 n,2*n,3*n...... ...
- VMware 获取该虚拟机的所有权失败
1. 虚拟机打开失败 VMware Workstation报错(打开虚拟机时出错:获取该虚拟机的所有权失败.主机上的某个应用程序正在使用该虚拟机.配置文件:D:\Ubuntu 16.04\Ubuntu ...
- 第一章 Python入门
一. 语言 计算机语言:人和计算机之间沟通的语言计算机语言: 按照级别分类:(越高级月进阶人类) 机器语言: 汇编语言: 助记符 ag. add 2 3 高级语言: c, PHP, java , .n ...
- 【java并发核心二】Exchanger的使用
类 Exchanger 的功能可以使2个线程之间传输数据,比生产者/消费者模式方便. Exchanger类的结构很简单,重点就是exchange()方法. exchange()方法是阻塞执行的,可以设 ...