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. Mac下Sublime text3无法安装Package Control及中文乱码问题

    sublime text3是一款轻量级的代码编辑器,我曾在Windows下配置过,但时间久了就忘了.这次是在mac上配置,在网上查了一些帖子,有的叙述不是很清楚,故记录一下详细过程. 在线安装: ht ...

  2. 前端node面试题之---对比JS和NodeJS的区别

    区别: 1.JS运行在浏览器端,用于用户的交互效果,NodeJS运行在服务器端,用于服务器的操作,例如,Web服务器创建,数据库的操作,文件的操作等 2.JS运行在浏览器端,存在多个JS解释器,存在兼 ...

  3. CSS3画菱形和平行四边形以及立方体

    利用CSS3中的transform属性画菱形和平行四边形 transform 实现2D或是3D的变形转换,通过transform可以实现对元素的四种变换:旋转.缩放.移动.倾斜 一.菱形 菱形的特点: ...

  4. localhost、127.0.0.1、本机ip、0.0.0.0 的区别

    1.各个地址 绑定到127.0.0.1的服务只能被本机访问. localhost是个域名,一般指向127.0.0.1这个ip,绑定到localhost的服务也只能被本机访问. 本机地址,指的是本机物理 ...

  5. 可恶!学了这么久的LCA,联考的题目却是LCA+树形DP!!!可恶|!!!这几天想学学树形DP吧!先来一道入门题HDU 1520 Anniversary party

    题目描述 某大学有N个职员,编号为1~N.他们之间有从属关系,也就是说他们的关系就像一棵以校长为根的树,父结点就是子结点的直接上司.现在有个周年庆宴会,宴会每邀请来一个职员都会增加一定的快乐指数Ri, ...

  6. 终于决定要开始写自己的博客了,先Mark一下

    终于决定要开始写了,但事实是,打开就觉得浪费时间,懒癌犯了

  7. 关于VMware 15搭建MacOS 10.14后无法播放在线视频和客户端视频的问题

    最近在自己的电脑上搭建了MacOS10.14系统,搭建是成功了,但是发现一个很坑的事,看视频发现黑屏.就是那种只有声音,没有视频的问题,在多个浏览器上和客户端都是一样的.百度了下,总结有2种可能,一是 ...

  8. (转载)《利用Python进行数据分析·第2版》电子书

    https://www.jianshu.com/p/04d180d90a3f https://www.jianshu.com/p/04d180d90a3f https://www.jianshu.co ...

  9. jq和js用法:入口写法

    jq和js入口写法demo: <!DOCTYPE html> <html lang="en"> <head> <meta charset= ...

  10. tikz: keep in mind these

    don't add % in title, xlabel, ylabel etc., use \%