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 ...
随机推荐
- with/as上下文管理器
# -*- coding: utf-8 -*- #python 27 #xiaodeng #Python学习手册 868 #with/as上下文管理器 #with语句的基本格式: with open( ...
- 触发器学习笔记(:new,:old用法)
触发器学习笔记(:new,:old用法) 触发器是数据库发生某个操作时自动运行的一类的程序 用于保持数据的完整性或记录数据库操作信息方面 触发器不能够被直接调用,只能够 ...
- 【laravel5.4】重定向带参数
1. 2.重定向回上一页面 3.返回上一页面带参数
- HDUOJ --2566
统计硬币 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- Java获取资源的路径
在Java中,有两种路径: 类路径 文件夹路径 使用类路径有两种方式: object.getClass().getResource()返回资源的URL MyClass.class.getResourc ...
- Java优化技巧
过早的优化是万恶之源. 优化了的代码可读性变差,可改性可适应性变差,可维护性变差. 远离过度优化,优化是个无底洞,把主要精力放在代码逻辑上. 优化的代码是活在当下的,是严重依赖硬件的,不利于表达永恒的 ...
- 自定义Microsoft Visual Studio 代码模板,增加公司和个人信息
C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\ItemTemplates\CSharp目录里面有各种新建模板分类: 修 ...
- Google Map 符号
符号 简介 如果您想在标记上使用基于矢量的图标,或者向多段线添加图像,便可使用符号. 标记支持使用光栅图像以及矢量图像.请参阅有关定制标记图标的文档. Symbol 是一种可显示在 Marker ...
- asp mvc @Html.CheckBox("sel",true) 往后台传值问题
@Html.CheckBox("sel",true) 生成2个输入,而不是一个,这是为什么呢? <input checked="checked" id=& ...
- OAF_OAF编译代码至应用详解(案例)
201-06-01 Created By BaoXinjian