回溯---Permutations II
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的更多相关文章
- Leetcode之回溯法专题-47. 全排列 II(Permutations II)
Leetcode之回溯法专题-47. 全排列 II(Permutations II) 给定一个可包含重复数字的序列,返回所有不重复的全排列. 示例: 输入: [1,1,2] 输出: [ [1,1,2] ...
- LeetCode46,47 Permutations, Permutations II
题目: LeetCode46 I Given a collection of distinct numbers, return all possible permutations. (Medium) ...
- LeetCode: Permutations II 解题报告
Permutations II Given a collection of numbers that might contain duplicates, return all possible uni ...
- 【leetcode】Permutations II
Permutations II Given a collection of numbers that might contain duplicates, return all possible uni ...
- LeetCode:Permutations, Permutations II(求全排列)
Permutations Given a collection of numbers, return all possible permutations. For example, [1,2,3] h ...
- leetcode总结:permutations, permutations II, next permutation, permutation sequence
Next Permutation: Implement next permutation, which rearranges numbers into the lexicographically ne ...
- leetcode Permutations II 无重全排列
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4051169.html 题目链接:leetcode Permutations II 无重全排 ...
- [Leetcode][Python]47: Permutations II
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 47: Permutations IIhttps://oj.leetcode. ...
- Permutations,Permutations II,Combinations
这是使用DFS来解数组类题的典型题目,像求子集,和为sum的k个数也是一个类型 解题步骤: 1:有哪些起点,例如,数组中的每个元素都有可能作为起点,那么用个for循环就可以了. 2:是否允许重复组合 ...
随机推荐
- A1036
输入n行不同学生的name性别id和成绩,输出成绩最高的女生名字和id,成绩最低的男生名字和id求出二者的差 如果有性别缺少,输出Absent并在结果行输出NA 注意变量不要搞混,可以用结构体……不过 ...
- C# 基础:DataTable操作、发邮件
本文出自:https://www.cnblogs.com/2186009311CFF/p/6865909.html DataTable操作 据参数删除为0的列:包括遍历.删除.取值 public st ...
- Element ui 中的表单提交按钮多次点击bug修复
- Java——容器(Map)
[Map接口]
- Spring Boot 中使用 spring-boot-devtools (使用 Gradle 作为构建工具)
Spring Boot 中使用 spring-boot-devtools (使用 Gradle 作为构建工具) 本文使用 Gradle 作为构建工具,关于 Gradle 构建工具,可以理解为是 Mav ...
- 大数据笔记(七)——Mapreduce程序的开发
一.分析Mapreduce程序开发的流程 1.图示过程 输入:HDFS文件 /input/data.txt Mapper阶段: K1:数据偏移量(以单词记)V1:行数据 K2:单词 V2:记一次数 ...
- 小白的git克隆流程clone
首先你进去你要存放代码的位置,比如将代码存放到D盘,然后在D盘中右键,点击Git Bash Here,就是说本地仓库要在D盘建立. 然后出现git 命令行界面,然后输入命令:git clone + 远 ...
- MySQL主从复制之半同步模式
MySQL主从复制之半同步模式 MySQL半同步介绍: 一般情况下MySQL默认复制模式为异步,何为异步?简单的说就是主服务器上的I/O threads 将binlog写入二进制日志中就返回给客户端一 ...
- FlexPaper做的类似百度文库的效果
这里有个误区,虽然我的截图这里有个FlexPaperViewer.swf, 但是这个文件还是要放在网站根目录一个. <%@ Page Language="C#" Auto ...
- windows传输文件到linux脚本
安装pscp https://www.chiark.greenend.org.uk/~sgtatham/putty/latest.html cmd脚本 @echo off rem 拷贝的文件名称 se ...