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的更多相关文章

  1. [Leetcode][Python]47: Permutations II

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 47: Permutations IIhttps://oj.leetcode. ...

  2. leetcode46. Permutations 、47. Permutations II、 剑指offer字符串的排列

    字符串排列和PermutationsII差不多 Permutations第一种解法: 这种方法从0开始遍历,通过visited来存储是否被访问到,level代表每次已经存储了多少个数字 class S ...

  3. 【LeetCode】47. Permutations II

    Permutations II Given a collection of numbers that might contain duplicates, return all possible uni ...

  4. leetCode 47.Permutations II (排列组合II) 解题思路和方法

    Permutations II  Given a collection of numbers that might contain duplicates, return all possible un ...

  5. [LeetCode] 47. Permutations II 全排列之二

    Given a collection of numbers that might contain duplicates, return all possible unique permutations ...

  6. 47. Permutations II (Back-Track, Sort)

    Given a collection of numbers that might contain duplicates, return all possible unique permutations ...

  7. 47. Permutations II (JAVA)

    Given a collection of numbers that might contain duplicates, return all possible unique permutations ...

  8. [leetcode] 47. Permutations II

    Given a collection of numbers that might contain duplicates, return all possible unique permutations ...

  9. LeetCode 【47. Permutations II】

    Given a collection of numbers that might contain duplicates, return all possible unique permutations ...

  10. 47. Permutations II

    题目: Given a collection of numbers that might contain duplicates, return all possible unique permutat ...

随机推荐

  1. UILabel UiButton 文字下面加下划线

    NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:@"直接进入" ...

  2. 滑动swipe的妙用

    转自:http://www.cnblogs.com/NEOCSL/archive/2013/03/04/2942861.html iterface ITouchable; function OnPic ...

  3. linux日常管理-抓包工具tcpdump和tshark

    抓包工具:查看什么数据占用网卡,把带宽跑满了. 命令:tcpdump 选项:host 指定IP port 指定端口 -c 指定包数量 -w 指定写入文件,不加显示的不是流量包而是流量走向 -nn 作用 ...

  4. 06_android虚拟机介绍

    分辨率不用选太高,否则会占用太大内存.你选高分辨率一跑起来会干掉你的500多MB的内存.1/8内存就没了.百分之97%或者是98%的设备都是ARM CPU.ARM自己不生产CPU,它生产的是一个标准的 ...

  5. 《精通Spring4.X企业应用开发实战》读后感第六章(属性编辑器)

  6. 基于http的多进程并发文件服务器

    1 可以掌握的知识点 (1) 线上部署时的守护应用 (2) 常规的文件操作,配置文件读取 (3) 网络编程,端口复用等文件 (4) 多进程知识 2 代码注释如下 test_httpd.h #inclu ...

  7. 除了BAT,计算机、软件专业的毕业生还有别的好去处吗?

    "学技术的同学应该关注36氪.pingwest,极客公园这些圈子里很有影响力的科技媒体" 转载--除了BAT,计算机.软件专业的毕业生还有别的好去处吗? 又到校招季,985理工科的 ...

  8. Asp.net Core 启动流程分析

    新建的.net core 程序启动本质上是一个控制台应用程序,所以它的入口在Main方法中,所以启动的开始时从Main方法开始. public class Program { public stati ...

  9. 【leetcode 106. 从中序与后序遍历序列构造二叉树】解题报告

    前往 中序,后序遍历构造二叉树, 中序,前序遍历构造二叉树 TreeNode* build(vector<int>& inorder, int l1, int r1, vector ...

  10. [CentOS7] minimal安装后 出现 没有ifconfig 无法ping 无法yum could not retrieve mirrorlist http://mirrorlist.centos.org/

    刚以minimal方式安装完CentOS,打算看下ip,结果ifconfig没找到(后来得知可以用ip addr查看本机ip) 于是yum grouplist, 结果出现could not retri ...