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的更多相关文章

  1. LeetCode刷题笔记和想法(C++)

    主要用于记录在LeetCode刷题的过程中学习到的一些思想和自己的想法,希望通过leetcode提升自己的编程素养 :p 高效leetcode刷题小诀窍(这只是目前对我自己而言的小方法,之后会根据自己 ...

  2. LeetCode刷题笔记 - 12. 整数转罗马数字

    学好算法很重要,然后要学好算法,大量的练习是必不可少的,LeetCode是我经常去的一个刷题网站,上面的题目非常详细,各个标签的题目都有,可以整体练习,本公众号后续会带大家做一做上面的算法题. 官方链 ...

  3. 18.9.10 LeetCode刷题笔记

    本人算法还是比较菜的,因此大部分在刷基础题,高手勿喷 选择Python进行刷题,因为坑少,所以不太想用CPP: 1.买股票的最佳时期2 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. ...

  4. Leetcode刷题笔记(双指针)

    1.何为双指针 双指针主要用来遍历数组,两个指针指向不同的元素,从而协同完成任务.我们也可以类比这个概念,推广到多个数组的多个指针. 若两个指针指向同一数组,遍历方向相同且不会相交,可以称之为滑动窗口 ...

  5. 【leetcode刷题笔记】Word Ladder II

    Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...

  6. 【leetcode刷题笔记】Palindrome Partitioning II

    Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...

  7. 【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 ...

  8. 【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 ...

  9. 【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, ...

随机推荐

  1. mysql主从从

    1.从官网下载安装percona-xtrabackup2.xtrabackup只能备份和恢复innodb的表,所以这里用innobackupex,可以实现对myisam和innodb的表在线备份和恢复 ...

  2. linux进程状态详解(转)

    Linux是一个多用户,多任务的系统,可以同时运行多个用户的多个程序,就必然会产生很多的进程,而每个进程会有不同的状态.  在下文将对进程的 R.S.D.T.Z.X 六种状态做个说明. PROCESS ...

  3. HDU 1014 Uniform Generator 题解

    找到规律之后本题就是水题了.只是找规律也不太easy的.证明这个规律成立更加不easy. 本题就是求step和mod假设GCD(最大公约数位1)那么就是Good Choice,否则为Bad Choic ...

  4. Windows 清除系统垃圾文件

    @echo off echo 正在清除系统垃圾文件,请稍等...... del /f /s /q %systemdrive%\*.tmp del /f /s /q %systemdrive%\*._m ...

  5. 征信报告页面的input验证收集

    https://ipcrs.pbccrc.org.cn/ function checkLoginName() { var loginName = $.trim($("#loginname&q ...

  6. java消息中间件之ActiveMQ初识

    目录 消息中间件简介 解耦合和异步 可靠性和高效性 JMS P2P Pub/Sub AMQP JMS和AMQP对比 常见消息中间件 ActiveMQ RabbitMQ Kafka 综合比较 标签(空格 ...

  7. YUV Player

    https://github.com/Yonsm/RawPlayer RawPlayer https://github.com/latelee/YUVPlayer YUVPlayer https:// ...

  8. Sqlserver建立Oracle的鏈接服務器

    --建立数据库链接服务器 EXEC sp_addlinkedserver @server =N'TestOracle', --要创建的链接服务器别名 @srvproduct=N'Oracle', -- ...

  9. iOS开发之 AES+Base64数据混合加密与解密

    2016-04-08 09:03 编辑: liubinqww 分类:iOS开发 来源:liubinqww 投稿 4 889     "APP的数据安全已经牵动着我们开发者的心,简单的MD5/ ...

  10. 详细的linux目录结构详细介绍

    详细的linux目录结构详细介绍 --树状目录结构图 下面红色字体为比较重要的目录 1./目录 目录 描述 / 第一层次结构的根,整个文件系统层次结构的根目录 /bin/ 需要在单用户模式可用的必要命 ...