47.Permutations II (Medium)](https://leetcode.com/problems/permutations-ii/description/)

[1,1,2] have the following unique permutations:
[[1,1,2], [1,2,1], [2,1,1]]

题目描述:

  数组元素可能包含重复元素,给出其数组元素的所有排列,不包含重复的排列。

思路分析:

  在实现上,和Permutations不同的是要先排序,然后在添加元素的时候,判断这个元素是否等于前一个元素,如果等于,并且前一个元素还未访问,那么就跳过 这个元素。

代码:

public List<List<Integer>>permuteUnique(int[]nums){
List<List<Integer>>res=new ArrayList<>();
if(nums==null||nums.length==0)
return res;
List<Integer>list=new ArrayList<>();
boolean[]visited=new boolean[nums.length];
Arrays.sort(nums); //对数组进行排序,方便后面进行剪枝
backtracking(nums,visited,res,list);
return res;
}
public void backtracking(int[]nums,boolean[]visited,List<List<Integer>>res,List<Integer>list){
if(list.size()==nums.length){
res.add(new ArrayList<>(list));
return ;
}
for(int i=0;i<nums.length;i++){
if(i!=0&&nums[i]==nums[i-1]&&visited[i-1]==false)
continue;//防止重复
if(visited[i])
continue;
visited[i]=true;
list.add(nums[i]);
backtracking(nums,visited,res,list);
list.remove(list.size()-1);
visited[i]=false;
}
}

回溯---Permutations II的更多相关文章

  1. Leetcode之回溯法专题-47. 全排列 II(Permutations II)

    Leetcode之回溯法专题-47. 全排列 II(Permutations II) 给定一个可包含重复数字的序列,返回所有不重复的全排列. 示例: 输入: [1,1,2] 输出: [ [1,1,2] ...

  2. LeetCode46,47 Permutations, Permutations II

    题目: LeetCode46 I Given a collection of distinct numbers, return all possible permutations. (Medium) ...

  3. LeetCode: Permutations II 解题报告

    Permutations II Given a collection of numbers that might contain duplicates, return all possible uni ...

  4. 【leetcode】Permutations II

    Permutations II Given a collection of numbers that might contain duplicates, return all possible uni ...

  5. LeetCode:Permutations, Permutations II(求全排列)

    Permutations Given a collection of numbers, return all possible permutations. For example, [1,2,3] h ...

  6. leetcode总结:permutations, permutations II, next permutation, permutation sequence

    Next Permutation: Implement next permutation, which rearranges numbers into the lexicographically ne ...

  7. leetcode Permutations II 无重全排列

    作者:jostree  转载请注明出处 http://www.cnblogs.com/jostree/p/4051169.html 题目链接:leetcode Permutations II 无重全排 ...

  8. [Leetcode][Python]47: Permutations II

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 47: Permutations IIhttps://oj.leetcode. ...

  9. Permutations,Permutations II,Combinations

    这是使用DFS来解数组类题的典型题目,像求子集,和为sum的k个数也是一个类型 解题步骤: 1:有哪些起点,例如,数组中的每个元素都有可能作为起点,那么用个for循环就可以了. 2:是否允许重复组合 ...

随机推荐

  1. hash详细介绍

    转发http://www.cnblogs.com/maybe2030/p/4719267.html (请转步这个网站,写得非常好)

  2. for-in语句和with语句、break和continue语句

    for-in语句 for-in语句是一种精准迭代语句,可以用来枚举对象的属性,用以遍历一个对象的全部属性. for…in声明用于对数组或者对象的属性进行循环操作: for…in循环中的代码每执行一次, ...

  3. computed属性和watcher

    computed属性 在模板中使用表达式是非常方便直接的,然而这只适用于简单的操作.在模板中放入太多的逻辑,会使模板过度膨胀和难以维护.例如: <div id="example&quo ...

  4. Go实现分布式外部排序

    Go实现分布式外部排序 项目路径: https://github.com/Draymonders/go_external_sort 默认读入文件: small.in 默认输出文件:small.out ...

  5. 【bzoj3343】教主的魔法

    *题目描述: 教主最近学会了一种神奇的魔法,能够使人长高.于是他准备演示给XMYZ信息组每个英雄看.于是N个英雄们又一次聚集在了一起,这次他们排成了一列,被编号为1.2.…….N. 每个人的身高一开始 ...

  6. 【CF1256F】Equalizing Two Strings(逆序对)

    题意:给定两个长度均为n且由小写字母组成的字符串,可以进行若干次操作,每次从两个串中分别选一个长度相等的子串进行翻转,问是否存在能使两串相等的一系列操作方案 n<=2e5 思路:首先如果每种字母 ...

  7. All-one Matrices

    All-one Matrices 单调栈 最大全一矩阵计数 #include<bits/stdc++.h> #define maxn 3005 using namespace std; # ...

  8. Redis之Java客户端Jedis

    导读 Redis不仅使用命令客户端来操作,而且可以使用程序客户端操作. 现在基本上主流的语言都有客户端支持,比如Java.C.C#.C++.php.Node.js.Go等. 在官方网站里列一些Java ...

  9. 2019年CCPC网络赛 HDU 6703 array【权值线段树】

    题目大意:给出一个n个元素的数组A,A中所有元素都是不重复的[1,n].有两种操作:1.将pos位置的元素+1e72.查询不属于[1,r]中的最小的>=k的值.强制在线. 题解因为数组中的值唯一 ...

  10. 解决IDEA输入法输入中文候选框不显示问题(亲测谷歌拼音完美解决问题)

    解决方法:关掉idea,进入idea的安装目录找到jre64文件夹重命名为jre642(随便什么名字都行)如下图 然后找到jdk安装目录下的jre文件复制到上图idea的安装目录下并改名为jre64 ...