【leetcode刷题笔记】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].
题解:跟Permutation差不多,只是这次会有重复的元素,如下图所示,如果只用DFS的话就会产生重复的排列:

上图中括号内的数字代表这个1是序列中第几个1。上述DFS产生了两个112,121和211。我们可以把两个112写成:1(1) 1(2) 2和1(2) 1(1) 2,可以看到,产生重复的原因是我们其实并不区分当前的1是序列中的第几个1,所以如果我们可以保证序列中相同的元素只按照在序列中序号递增的顺序出现,就可以避免重复了。比如上述的112,我们只允许1(1) 1(2) 2这种情况,而步允许1(2) 1(1) 2这种情况出现。
只需要在判断的时候加上如下所示高亮部分的代码就可以做到这一点:
public class Solution {
public List<List<Integer>> permuteUnique(int[] num) {
List<List<Integer>> answerList = new ArrayList<List<Integer>>();
ArrayList<Integer> currentList = new ArrayList<Integer>();
boolean[] visited = new boolean[num.length];
Arrays.sort(num);
DS(answerList, visited, num, currentList);
return answerList;
}
public void DS(List<List<Integer>> answerList,boolean[] visited,int[] num,ArrayList<Integer> currentList){
boolean find = true;
for(int i = 0;i < num.length;i++){
if(i-1>=0 && num[i]== num[i-1] && visited[i-1] == false)
continue;
if(!visited[i]){
currentList.add(num[i]);
visited[i]= true;
DS(answerList, visited, num, currentList);
visited[i]= false;
currentList.remove(currentList.size()-1);
find = false;
}
}
if(find){
ArrayList <Integer> temp = new ArrayList<Integer>(currentList);
answerList.add(temp);
}
}
}
【leetcode刷题笔记】Permutations II的更多相关文章
- LeetCode刷题笔记和想法(C++)
主要用于记录在LeetCode刷题的过程中学习到的一些思想和自己的想法,希望通过leetcode提升自己的编程素养 :p 高效leetcode刷题小诀窍(这只是目前对我自己而言的小方法,之后会根据自己 ...
- LeetCode刷题笔记 - 12. 整数转罗马数字
学好算法很重要,然后要学好算法,大量的练习是必不可少的,LeetCode是我经常去的一个刷题网站,上面的题目非常详细,各个标签的题目都有,可以整体练习,本公众号后续会带大家做一做上面的算法题. 官方链 ...
- 18.9.10 LeetCode刷题笔记
本人算法还是比较菜的,因此大部分在刷基础题,高手勿喷 选择Python进行刷题,因为坑少,所以不太想用CPP: 1.买股票的最佳时期2 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. ...
- Leetcode刷题笔记(双指针)
1.何为双指针 双指针主要用来遍历数组,两个指针指向不同的元素,从而协同完成任务.我们也可以类比这个概念,推广到多个数组的多个指针. 若两个指针指向同一数组,遍历方向相同且不会相交,可以称之为滑动窗口 ...
- 【leetcode刷题笔记】Word Ladder II
Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...
- 【leetcode刷题笔记】Palindrome Partitioning II
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
- 【leetcode刷题笔记】Best Time to Buy and Sell Stock II
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- 【leetcode刷题笔记】Populating Next Right Pointers in Each Node II
What if the given tree could be any binary tree? Would your previous solution still work? Note: You ...
- 【leetcode刷题笔记】Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3, ...
随机推荐
- C# DataTable 转 List(大家进来讨论讨论)
C# DataTable 转 List 方法,网上有好多,之前也收集了,感觉这个也不错,重要是自己要领会这里面的代码含义. 接不来我就把代码贴出来分享一下,大家觉得如果不好,请留言我,我来改进. us ...
- java精确除法计算,四舍五入 Java问题通用解决代码
主要用java.math.BigDecimal工具类实现,想要了解BigDecimal类可以看java api 正式版: public static Double divide() ...
- 2.JAVA编程思想——一切都是对象
一切都是对象 欢迎转载.转载请标明出处:http://blog.csdn.net/notbaron/article/details/51040221 虽然以C++为基础,但 Java 是一种更纯粹的面 ...
- unity4.6 Beta版 UI控件之Button
近期需求,须要用到4.6版本号uGui了,所以抽时间来学习学习,就UI控件在Unity工具里创建预设这块来说相比較于NGUI,我认为是没有什么太大的差别的. 比方:Canvas--Camera . T ...
- Iwfu-GitHubclient使用
Git/GitHub介绍 GitHub是著名的项目托管平台,有关Git和GitHub的介绍參考以下链接: Git介绍 url=OlagjwbaAdSJ2pjckgJCLBYd-LFFTDBriRnLt ...
- 第一次接触solr的过程记录
1.以solr-4.6.0.tgz为例进行学习 2.第一步,看的是 tutorial.html(位于solr-4.6.0/docs目录),默认solr以jetty作为servlet容器 3.但是,如果 ...
- 把flask部署到服务器
1.新建一个wsgi.py文件 # -*- coding:utf-8 -*- import sys from os.path import abspath from os.path import di ...
- phalcon builder get raw sql
$this->modelsManager->createBuilder()->from('table')->where('a = "a"')->lim ...
- mysql主从:主键冲突问题
1.检查从库 show slave status \G; Slave_IO_Running: YesSlave_SQL_Running: No 2.出现类似如下的报错: Last_SQL_Error: ...
- linux 常用的17个性能指标
1.Average load:Average number of processes simultaneously in Ready state during the last minute. 上 ...