【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个数字换, 但是不换重复的数字 第一个数字和第三个数字就不换了

class Solution {
public:
    vector<vector<int> > permuteUnique(vector<int> &num) {
        vector<vector<int>> ans;
        if(num.empty())
            return ans;

        permute(ans,  num, 0);
        return ans;
    }
   //不知道为什么 注释掉的这种 在我自己的电脑上就ok 但是提交就总是Output Limit Exceeded??
    //void permute(vector<vector<int>> &ans, vector<int> num, int k)
    //{
    //    if(k >= num.size())
    //    {
    //        ans.push_back(num);
    //        return;
    //    }
    //    for(int j = k; j < num.size(); j++) //和自己或后面交换
    //    {
    //         if (j > k && num[j] == num[j-1]) continue;    //prevent duplicates
    //         swap(num[k], num[j]);
    //         permute(ans, num, k + 1);
    //         swap(num[k], num[j]);
    //    }
    //}
    void permute(vector<vector<int>> &ans, vector<int> num, int k)
    {
        if(k >= num.size())
        {
            ans.push_back(num);
            return;
        }
        vector<int> hash;
        for(int j = k; j < num.size(); j++) //和自己或后面交换
        {
            if(find(hash.begin(), hash.end(), num[j]) == hash.end())
            {
                hash.push_back(num[j]);
                swap(num[k], num[j]);
                permute(ans, num, k + 1);
                swap(num[k], num[j]);
            }
        }
    }
};
时间: 2024-10-12 13:32:50

【leetcode】Permutations II (middle)的相关文章

【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,当前排列数

【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】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】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

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】 Permutation Sequence (middle)

The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the permutations in order,We get the following sequence (ie, for n = 3): "123" "132" "213" "231" "312" "3