【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].

首先分析一下与Permutations有何差异。

记当前位置为start,当前排列数组为cur

1、cur[start]与cur[start]值相同的元素交换位置会产生大量重复。

如:1,3,2,1

两个1互换位置之后,后续的所有排列都是重复的。

2、cur[start]与其他相同值的元素多次交换位置会产生大量重复。

如:1,2,3,2

1与两个2互换位置后,后续的所有排列都是重复的。

因此改变在于:

对于同一个值,只交换一次,否则跳过。

为了保证这一点,必须对cur数组start位置之后的元素排序,这样可以用while循环跳过已经交换过的元素。

若不排序会产生如下问题:

0,0,1,9 --> 9,0,1,0

0就不连续了,又会产生重复。

class Solution
{
public:
    vector<vector<int> > permuteUnique(vector<int> &num)
    {
        vector<vector<int> > result;
        vector<int> cur = num;
        Helper(result, cur, 0);
        return result;
    }
    void Helper(vector<vector<int> >& result, vector<int> cur, int start)
    {
        sort(cur.begin()+start, cur.end());    //must!
        if(start == cur.size()-1)
        {
            result.push_back(cur);
        }
        else
        {
            Helper(result, cur, start+1);
            int last = cur[start];
            int i = start+1;
            while(true)
            {
                while(i < cur.size() && cur[i] == last)
                    i ++;
                if(i < cur.size())
                {
                    swap(cur[start], cur[i]);
                    Helper(result, cur, start+1);
                    swap(cur[i], cur[start]);
                    last = cur[i];
                }
                else
                    break;
            }
        }
    }
};

时间: 2024-10-21 19:11:34

【LeetCode】Permutations II的相关文章

【leetcode】Permutations II (middle)

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]. 思路: 找规律递归 一个数跟自己后面的数字交换如 1 2 2 第一个数字和第2个数字换, 但是不换重复的数字 第一个

【leetcode】N-queens II

问题: 返回N皇后问题解的个数. 分析: 详见 N-queens 实现: bool nextPermutation(vector<int> &num) { int i = num.size() - 1; while (i >= 1) { if(num[i] > num[i - 1]) { --i; int ii = num.size() - 1; while (ii > i && num[ii] <= num[i]) --ii; if(ii &g

【LeetCode】Permutations

Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the following permutations:[1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], and [3,2,1]. public class Solution { public ArrayList<ArrayList<Integer>> permute

【LeetCode】Subsets II 解题报告

[题目] Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: Elements in a subset must be in non-descending order. The solution set must not contain duplicate subsets. For example, If S = [1,2,2], a solutio

【LeetCode】Permutations 解题报告

全排列问题.常用的排列生成算法有序数法.字典序法.换位法(Johnson(Johnson-Trotter).轮转法以及Shift cursor cursor* (Gao & Wang)法. [题目] Given a collection of numbers, return all possible permutations. For example, [1,2,3] have the following permutations: [1,2,3], [1,3,2], [2,1,3], [2,3

【leetcode】Subsets II

Subsets II Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: Elements in a subset must be in non-descending order. The solution set must not contain duplicate subsets. For example,If S = [1,2,2], a so

【leetcode】Permutations (middle)

Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the following permutations:[1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], and [3,2,1]. 求没有重复数字的全排列 思路:用的标准回溯法,一次AC class Solution { public: vector<vector<int>

【leetcode】Subsets II (middle) ☆

Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: Elements in a subset must be in non-descending order. The solution set must not contain duplicate subsets. For example,If S = [1,2,2], a solution is:

【LeetCode】Next Permutation 解题报告

[题目] Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers. If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending order). The repl