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. day02记

    一.Tomcat搭建 1.service和controller分别搭建独立的Tomcat且port不一致 2.部署项目选择带有exploded的 3.运行时应先启动service再启动controll ...

  2. 一个错误导致懂了mac系统的PATH环境变量

    一个完全不懂mac系统的强迫症小白,由于搭建环境都按照百度走,所以在执行命令echo $PATH查看PATH内容时发现怎么有这样一串东西 /usr/local/bin:/usr/bin:/bin:/u ...

  3. celery在项目中的使用

    1 关于celery是一个处理异步耗时任务的框架 由 worker 和broker 和store 3部分组成 worker是来处理消息的工人 broker是来存储请求消息的仓库 store是用来存储结 ...

  4. HTML和CSS实现图片翻转效果

    实现图片翻转,首先来分析一下我们希望实现的是怎样的翻转效果?又该如何去实现呢? 一.希望实现的效果 页面上的图片在光标悬停在上面的时候会发生翻转效果,翻转过后显示出背面的说明文字. 鼠标没有悬停在上面 ...

  5. 自动配置/切换/查看JDK环境变量

    最近老是需要几个版本的JDK切换工作,于是网上收集资料整理,自己写了一个:自动配置/切换/查看JDK环境变量的批处理脚本.顺带3个JDK版本分别是:jdk1.6.0_43,jdk1.7.0_80,jd ...

  6. SQL server 字段合并CAST(org_no AS VARCHAR(20))+CAST(page_no AS VARCHAR(20))+CAST(djlb_no AS VARCHAR(20)))

    sql server 字段合并(CAST) ---------------------- select (CAST(org_no AS VARCHAR(20))+CAST(page_no AS VAR ...

  7. C中的system函数

    windows操作系统下system () 函数详解(主要是在C语言中的应用) 函数名: system 功 能: 发出一个DOS命令 用 法: int system(char *command); s ...

  8. formdata方式上传文件,支持大文件分割上传

    1.upload.html <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/html"> <h ...

  9. Behaviour Tree Service 中的几个函数

    Service中可以override的函数有8个,因为每个函数都有个AI版本的,所以实际上是4组函数,AI版本的和非AI版本基本一样, 他们分别是: Receive Search Start (AI) ...

  10. [CSP-S模拟测试]:星际旅行(欧拉路)

    题目传送门(内部题4) 输入格式 第一行两个整数$n,m$,表示行星和虫洞的数量.接下来$m$行,每行两个整数$u,v$,表示存在一个双向虫洞直接连接$u$和$v$.每一个虫洞最多会被描述一次. 输出 ...