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);
        }
    }
}

方法二

优化一下数据结构

时间: 2024-12-12 06:28:40

leetcoe--47. Permutations II的相关文章

[Lintcode]16. Permutations II/[Leetcode]47. Permutations II

16. Permutations II/47. Permutations II 本题难度: Medium Topic: Search & Recursion Description Given a list of numbers with duplicate number in it. Find all unique permutations. Example Example 1: Input: [1,1] Output: [ [1,1]] Example 2: Input: [1,2,2] O

leetCode 47.Permutations II (排列组合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]. 思路:这题相比于上一题,是去除了反复项. 代码上与上题略有区别.详细代码例如以下

47. 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]. 链接: http://leetcode.com/problems/permutations-ii/ 4/15

47. Permutations II java solutions

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] ] 1 public class Solution { 2 List<List<Integer>> ans

LeetCode 【47. 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], [2,1,1] ] 思路1.这题与Permutations的区别在于他允许重复数字,最简单的就是利用1的结果,保存在一个set中,去重复

47. Permutations II (java )

题目: Given a collection of numbers that might contain duplicates, return all possible unique permutations. 解析: 编码:

[leedcode 47] 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]. public class Solution { List<Integer> seq; List<List&l

47. 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], [2,1,1] ] 与上一题不同,就是在19行加个判断即可. 1 class Solution(object): 2 def __ini

(待解决,效率低下)47. Permutations II C++回溯法

思路是在相似题Permutations的基础上,将结果放到set中,利用set容器不会出现重复元素的特性,得到所需结果 但是利用代码中的/* */部分通过迭代器遍历set将set中的元素放在一个新的vector中时,会出现memory limit exceeded错误(原因??) 上网查找后发现可以直接通过return vector<vector<int>> (mySet.begin(),mySet.end())得到结果,并且代码通过. class Solution { publi

19.2.7 [LeetCode 47] Permutations II

Given a collection of numbers that might contain duplicates, return all possible unique permutations. Example: Input: [1,1,2] Output: [ [1,1,2], [1,2,1], [2,1,1] ] 1 class Solution { 2 public: 3 vector<vector<int>> permuteUnique(vector<int&