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. java调用存储过程的方式

    1.问号是入参和出参,出参要指定类型 CallableStatement pstmt = conn.prepareCall("{call dbo.UP_CodeUp_***(?,?,?,?, ...

  2. 关于vs2019

    一.vs2019中的MFC 在想创建一个基于对话的应用时找不着模版了,这下可慌了,试遍了已有的各个模版都没要,要么就是缺少头文件,我在想是不是少安装了什么选项.重装了相关模块,最后又核对了一遍,都对. ...

  3. php大文件上传解决方案

    PHP用超级全局变量数组$_FILES来记录文件上传相关信息的. 1.file_uploads=on/off 是否允许通过http方式上传文件 2.max_execution_time=30 允许脚本 ...

  4. 老男孩python3.5全栈开发第9期+课件笔记(1-15部全 共125天完整无加密)

    点击了解更多Python课程>>> 老男孩python3.5全栈开发第9期+课件笔记(1-15部全 共125天完整无加密)大小:236G 此课程为老男孩全栈开发最新完结课程,适合零基 ...

  5. Android环境配置之正式版AndroidStudio1.0

    昨天看见 Android Studio 1.0 正式版本发布了:心里挺高兴的. 算是忠实用户了吧,从去年开发者大会一开始出现 AS 后就开始使用了:也是从那时开始就基本没有用过 Eclipse 了:一 ...

  6. ijkplayer阅读学习笔记之从代码上看播放流程

    http://blog.csdn.net/i_do_can/article/details/51374732

  7. TensorFlow 源码编译安装

    ## Install prerequisites (rhel) yum install numpy python-devel python-wheel python-mock ## Install B ...

  8. ARM非对齐访问和Alignment Fault

    1.指令对齐 A64指令必须word对齐.尝试在非对齐地址取值会触发PC alignment fault. 1.1.PC alignment checking PC(Program Counter)寄 ...

  9. java.lang.NoClassDefFoundError: com/opensymphony/xwork2/util/finder/DefaultClassFinder$InfoBuildingV 解决方法

    问题:严重: Unable to read class [com.spml.action.AddUserAction]java.lang.NoClassDefFoundError: com/opens ...

  10. 阶段1 语言基础+高级_1-3-Java语言高级_06-File类与IO流_07 缓冲流_4_缓冲流的效率测试_复制文件

    把之前文件复制的代码复制到这里 一个字节一个字节的读取,复制文件 byte数组的形式 缓冲流测试 数组缓冲