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. Python:生成器函数

    生成器函数:包含yield语句的函数: 生成器对象:生成器对象和迭代器对象行为相似,都支持可迭代接口:__next__(),若想执行生成器函数内部语句,则需要迭代协议’ A.生成器函数被调用时,并不会 ...

  2. lvs-nat搭建httpd

    拓扑图: #172.16.252.10 [root@~ localhost]#route -n Kernel IP routing table Destination Gateway Genmask ...

  3. ORM查询相关

    一.多对多的正反向查询 class Class(models.Model): name = models.CharField(max_length=32,verbose_name="班级名& ...

  4. Tomcat 服务器详解

    工具/原料 1.JDK:版本为jdk-7-windows-i586.exe  下载地址   http://www.oracle.com/technetwork/java/javase/download ...

  5. Java虚拟机内存配置

    在做java开发时尤其是大型软件开发时经常会遇到内存溢出的问题,比如说OutOfMemoryError等.这是个让开发人员很痛苦.也很纠结的问题,因为我们有时不知道什么样的操作导致了这种问题的发生.所 ...

  6. QueryString

  7. 利用css实现鼠标经过元素,下划线由中间向两边展开

    代码如下: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...

  8. Gson应用:从json格式简单字符串中获取value

    import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStreamReader; i ...

  9. vs2013提交项目到github

    提交项目之前必须先安装Git,下载地址:https://git-scm.com/download/win 1.登录Github后,在顶部导航栏选择New repository: 2.打开Create ...

  10. 给 asp.net core 写一个简单的健康检查

    给 asp.net core 写一个简单的健康检查 Intro 健康检查可以帮助我们知道应用的当前状态是不是处于良好状态,现在无论是 docker 还是 k8s 还是现在大多数的服务注册发现大多都提供 ...