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) ...
随机推荐
- win10下vscode配置sftp
sftp配置 1.在vscode中下载sftp插件 在vscode中快捷键 ctrl+shift+P 打开指令窗口,输入extension:install,回车,左侧即打开扩展安装的界面 在搜索框中 ...
- poj1195二维树状数组模板
二维树状数组和一维的也差不多,改一下add和query函数即可:即按行修改,行内单点修改即可 /* 二维树状数组,询问一个二维区间内的数之和 */ #include<iostream> # ...
- pytest一:pytest 框架介绍
pytest 是 python 的一种单元测试框架,与python 自带的 unittest测试框架类似,但是比 unittest 框架使用起来更简洁,效率更高.根据pytest 的官方网站介绍,它具 ...
- For each loop in Native C++
今天发现 for each 语法居然可以直接编译通过,之前还以为只有开了/clr才可以支持.查了一下资料发现ms从vs2005就已经支持了.虽然不符合标准不过用着确实方便啊,必须记录一下. 具体看这里 ...
- find算法
find(beg, end, val) :根据equal操作符,循序查找[first, last)内所有的元素,找出第一个匹配“等同条件者”.如果找到,就返回一个指向钙元素的迭代器,否者返回迭代器 ...
- Oracle 11.2.0.4 For Windows 64bit+32bit 数据库
1.Oracle11G 32BIT介质官方链接 适用于Windows 32bit的Oracle Database 11G 第2版U4(11.2.0.4)Oracle11.2.0.4 Windows3 ...
- Hadoop Yarn环境配置
抄一个可行的Hadoop Yarn环境配置.用的官方的2.2.0版本. http://www.jdon.com/bigdata/yarn.html Hadoop 2.2新特性 将Mapreduce框架 ...
- 3.Django| 视图层| 模板层
1.视图函数 文件在view_demo 一个视图函数简称视图,是一个简单的Python 函数,它接受Web请求并且返回Web响应.响应可以是一张网页的HTML内容,一个重定向,一个404错误,一个XM ...
- 【Java】 剑指offer(7) 二叉树的下一个结点
本文参考自<剑指offer>一书,代码采用Java语言. 更多:<剑指Offer>Java实现合集 题目 给定一棵二叉树和其中的一个结点,如何找出中序遍历顺序的下一个结点? ...
- 详解php中serialize()和unserialize()函数
php的serialize()函数和unserialize()函数 适用情境:serialize()返回字符串,此字符串包含了表示value的字节流,可以存储于任何地方.这有利于存储或传递 PHP 的 ...