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. UIViewController新方法的使用(transitionFromViewController:toViewController:duration:options:animations:completion:)

    iOS5中,UIViewController新添加了几个方法: - (void)addChildViewController:(UIViewController *)childController N ...

  2. HDU 1014 Uniform Generator 题解

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

  3. 在caffe中用训练好的 caffemodel 来分类新的图片所遇到的问题

    结合之前的博客: http://www.cnblogs.com/Allen-rg/p/5834551.html#3949333 用caffemodel去测试单通道的图像(mnist数据集)时,出现了问 ...

  4. 压力测试工具集合(ab,webbench,Siege,http_load,Web Application Stress)

    压力测试工具集合(ab,webbench,Siege,http_load,Web Application Stress) 1 Apache附带的工具ab ab的全称是ApacheBench,是Apac ...

  5. OpenCV for Python 学习笔记 三

    给源图像增加边界 cv2.copyMakeBorder(src,top, bottom, left, right ,borderType,value) src:源图像 top,bottem,left, ...

  6. Amzaon EC2虚拟化技术演进:从 Xen 到 Nitro

      今年2月,由光环新网运营的 AWS 中国(北京)区域和由西云数据运营的 AWS 中国 (宁夏)区域发布新的实例类型,新的实例类型包括 C5.C5d.R5.R5d.除了这四种之外,在AWS国外部分区 ...

  7. 【Python+selenium】之奇怪问题总结

    问题1: <_io.TextIOWrapper name='<stderr>' mode='w' encoding='UTF-8'> Time Elapsed: 0:00:04 ...

  8. [LeetCode] Remove Duplicates from Sorted Array II [27]

    题目 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ex ...

  9. 设置tableView背景颜色

    [treeTableView setAutoresizingMask:UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight] ...

  10. os模块,sys模块

    # os模块 # os模块是与操作系统交互的一个接口 ''' 和工作目录相关: os.getcwd() 获取当前工作目录,即当前python脚本工作的目录路径(在什么地方执行就是那个文件的路径) os ...