LeetCode: Permutations II 解题报告
Permutations II
Given a collection of numbers that might contain duplicates, return all possible unique permutations.
For example,
[1,1,2] have the following unique permutations:
[1,1,2], [1,2,1], and [2,1,1].

SOLUTION 1:
还是经典的递归模板。需要处理的情况是:我们先把Num排序,然后只能连续地选,这样就可以避免生成重复的solution.
例子:1 2 3 4 4 4 5 6 7 8
444这个的选法只能:4, 44, 444连续这三种选法
我们用一个visit的数组来记录哪些是选过的。
public class Solution {
public List<List<Integer>> permuteUnique(int[] num) {
List<List<Integer>> ret = new ArrayList<List<Integer>>();
if (num == null || num.length == 0) {
return ret;
}
// For deal with the duplicate solution, we should sort it.
Arrays.sort(num);
boolean[] visit = new boolean[num.length];
dfs(num, new ArrayList<Integer>(), ret, visit);
return ret;
}
public void dfs(int[] num, ArrayList<Integer> path, List<List<Integer>> ret, boolean[] visit) {
int len = num.length;
if (path.size() == len) {
ret.add(new ArrayList<Integer>(path));
return;
}
for (int i = 0; i < len; i++) {
// 只能连续地选,这样就可以避免生成重复的solution.
// 例子:1 2 3 4 4 4 5 6 7 8
// 444这个的选法只能:4, 44, 444连续这三种选法
if (visit[i] || (i != 0 && visit[i - 1] && num[i] == num[i - 1])) {
continue;
}
// 递归以及回溯
visit[i] = true;
path.add(num[i]);
dfs(num, path, ret, visit);
path.remove(path.size() - 1);
visit[i] = false;
}
}
}
SOLUTION 2:
用一个pre来记录选过的值,也可以达到同样的目的,只取第一个可以取到的位置,后面再取也是一样的解。用pre记下来,后面就不要再取同样的值了
// SOLUTION 2:
// 使用一个pre来记录。只取第一个可以取的位置
public void dfs(int[] num, ArrayList<Integer> path, List<List<Integer>> ret, boolean[] visit) {
int len = num.length;
if (path.size() == len) {
ret.add(new ArrayList<Integer>(path));
return;
} long pre = Long.MIN_VALUE;
for (int i = 0; i < len; i++) {
int n = num[i];
// 只取第一个可取的位置,因为别的位置取到的也没有区别
if (visit[i] || pre == n) {
continue;
}
pre = n; // 递归以及回溯
visit[i] = true;
path.add(n);
dfs(num, path, ret, visit);
path.remove(path.size() - 1);
visit[i] = false;
}
}
GITHUB:
https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/permutation/PermuteUnique.java
LeetCode: Permutations II 解题报告的更多相关文章
- 【LeetCode】Permutations II 解题报告
[题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...
- 【LeetCode】47. Permutations II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:递归 方法二:回溯法 日期 题目地址:htt ...
- LeetCode: N-Queens II 解题报告
N-Queens II (LEVEL 4 难度级别,最高级5) Follow up for N-Queens problem.
- LeetCode: Combination Sum 解题报告
Combination Sum Combination Sum Total Accepted: 25850 Total Submissions: 96391 My Submissions Questi ...
- LeetCode: Unique Paths II 解题报告
Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution Fol ...
- 【LeetCode】Pascal's Triangle II 解题报告
[LeetCode]Pascal's Triangle II 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/pascals-tr ...
- 【LeetCode】375. Guess Number Higher or Lower II 解题报告(Python)
[LeetCode]375. Guess Number Higher or Lower II 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...
- 【LeetCode】731. My Calendar II 解题报告(Python)
[LeetCode]731. My Calendar II 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题 ...
- 【LeetCode】522. Longest Uncommon Subsequence II 解题报告(Python)
[LeetCode]522. Longest Uncommon Subsequence II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemin ...
随机推荐
- 安装调试Installing Odoo
来自odoo的安装步骤 There are mutliple ways to install Odoo, or not install it at all, depending on the inte ...
- Android4.0-4.4 加入实体按键振动支持的方法(java + smali版本号)
有些手机比方泛泰A820L, 泛泰A890 A900 以及Nubia Z5S 和Z5S mini具有实体按键(这里所说的实体按键是相对于虚拟按键而言, 包括物理按键和触摸屏上多出来的触摸实体按键), ...
- @Autowired(required = false)
标记在 方法上的时候,它会根据类型去spring容器中寻找 对于的形参并且注入. @Repository(value="userDao") public class UserDao ...
- eclipse新建maven工程的各种坑
尽量按照最后强烈推荐的那篇创建maven工程. 1.jsp文件头报错 2.xml配置文件头红叉 3.Archive for required library...blabla 4.pom依赖出错 5 ...
- JSP开发中对jstl的引用方式(标签库引用)
创建标签库引用文件taglibs.inc 一 采用本地标签库的taglibs.inc文件 <%--struts库标签 --%> <%@ taglib uri="/WEB-I ...
- C# 默认参数/可选参数需要注意
在使用C#的默认参数/可选参数的时候,需要注意,参数的默认值是在编译的时候,自动加入调用方的. 如我有这样一个方法: public class Name { public void Test(Bool ...
- go 学习 ---golang命令
1.GO命令一览 GO提供了很多命令,包括打包.格式化代码.文档生成.下载第三方包等等诸多功能,我们可以通过在控制台下执行 go 来查看内置的所有命令 下面来逐个介绍,也可以详细参考 https:// ...
- ulbuntu 安装配置 java
一.下载JDK 下载地址: https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151 ...
- Kruskal算法 - C语言详解
最小生成树 在含有n个顶点的连通图中选择n-1条边,构成一棵极小连通子图,并使该连通子图中n-1条边上权值之和达到最小,则称其为连通网的最小生成树. 例如,对于如上图G4所示的连通网可以有多棵权值总 ...
- 浅析I/O模型
以下是本文的目录大纲: 一.什么是同步?什么是异步? 二.什么是阻塞?什么是非阻塞? 三.什么是阻塞IO?什么是非阻塞IO? 四.什么是同步IO?什么是异步IO? 五.五种IO模型 六.两种高性能IO ...