leetcoe--47. Permutations II
1、问题描述
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],
[2,1,1]
]
2、边界条件:重复数字,去重方法是重要考察点,采用事前去重是有效率的。
3、思路:排列问题,递归的方法实现,先取一个数放在位置1,然后剩下N-1个位置,再依次放入;循环,取第二个数放在位置1。
4、代码实现
方法一
class Solution {
public List<List<Integer>> permuteUnique(int[] nums) {
List<List<Integer>> results = new ArrayList<>();
Arrays.sort(nums);
List<Integer> numList = new ArrayList<>();
for (int i = 0; i < nums.length; i++) {
numList.add(nums[i]);
}
permuteUnique(results, new ArrayList<Integer>(), numList);
return results;
}
public void permuteUnique(List<List<Integer>> results, List<Integer> cur,
List<Integer> numList) {
if (0 == numList.size()) {
List<Integer> result = new ArrayList<>(cur);
results.add(result);
return;
}
for (int i = 0; i < numList.size(); i++) {
if (i != 0 && numList.get(i) == numList.get(i - 1)) { //事前去重
continue;
}
cur.add(numList.get(i));
numList.remove(i);
permuteUnique(results, cur, numList);
numList.add(i, cur.get(cur.size() - 1));
cur.remove(cur.size() - 1);
}
}
}
方法二
优化一下数据结构
leetcoe--47. Permutations II的更多相关文章
- [Leetcode][Python]47: Permutations II
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 47: Permutations IIhttps://oj.leetcode. ...
- leetcode46. Permutations 、47. Permutations II、 剑指offer字符串的排列
字符串排列和PermutationsII差不多 Permutations第一种解法: 这种方法从0开始遍历,通过visited来存储是否被访问到,level代表每次已经存储了多少个数字 class S ...
- 【LeetCode】47. Permutations II
Permutations II Given a collection of numbers that might contain duplicates, return all possible uni ...
- leetCode 47.Permutations II (排列组合II) 解题思路和方法
Permutations II Given a collection of numbers that might contain duplicates, return all possible un ...
- [LeetCode] 47. Permutations II 全排列之二
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- 47. Permutations II (Back-Track, Sort)
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- 47. Permutations II (JAVA)
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- [leetcode] 47. Permutations II
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- LeetCode 【47. Permutations II】
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- 47. Permutations II
题目: Given a collection of numbers that might contain duplicates, return all possible unique permutat ...
随机推荐
- Azure blob Storage Snapshot
用户虚拟机硬盘的备份是客户在部署云应用中是一个非常重要的部分. 目前有多种平台的备份方法: 捕获镜像:可以采用Capture的方式(powershell命令为Save-AzureVMImage)捕获虚 ...
- TP5隐藏url中的index.php
在public文件夹下,有个.htacess文件,没有则新建一个, 如果已有这个文件,原文件内容如下: <IfModule mod_rewrite.c> Options +FollowSy ...
- qt数据库sql语句使用c++中的变量
void SerialWidget::on_btnMysql_clicked() { qDebug()<<QSqlDatabase::drivers()<<endl; /*列出 ...
- PopupWindow --- 弹出底部窗体
第一步 : 布局文件 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:a ...
- 使用mahout fpgrowth算法求关联规则
使用mahout fpgrowth 首先,这篇文章的内容大部分取自国外一篇博客Finding association rules with Mahout Frequent Pattern Mining ...
- 15、Linux 文件属性和测试( chgrp,chown,chmod和-e -f -d -s
一.更改文件属性 1.chgrp:更改文件属组 语法: chgrp [-R] 属组名文件名 参数选项 -R:递归更改文件属组,就是在更改某个目录文件的属组时,如果加上-R的参数,那么该目录下的所有文件 ...
- 24.command-executor
这里先给出题目链接: https://command-executor.hackme.inndy.tw/ 这是一道不错的ctf题,首先说一下考察点: 文件包含读源码 代码分析结合CVE CVE导致的命 ...
- [转载]Java程序员掌握的10大项知识体系--精通太难说出口
1.语法:必须比较熟悉,在写代码的时候IDE的编辑器对某一行报错应该能够根据报错信息知道是什么样的语法错误并且知道任何修正. 2.命令:必须熟悉JDK带的一些常用命令及其常用选项,命令至少需要熟悉:a ...
- 【Qt文档阅读】Window and Dialog Widgets
Window and Dialog Widgets 没有嵌入到父控件中的控件(widget)称之为窗口(window).通常窗口带有边框和标题栏. Windows通常集成到桌面环境中,并且在某种程度上 ...
- C#----接口与抽象类
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace 接口 { ...