【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> > permute(vector<int> &num) {
        vector<vector<int>> ans;
        if(num.empty())
        {
            return ans;
        }

        vector<int> X(num.size());
        vector<vector<int>> S(num.size());
        int k = 0;
        S[k] = num;

        while(k >= 0)
        {
            while(!S[k].empty())
            {
                X[k] = S[k].back();
                S[k].pop_back();
                if(k < num.size() - 1)
                {
                    k++;
                    S[k] = num;
                    for(int i = 0; i < k; i++)
                    {
                        vector<int>::iterator  it;
                        if((it = find(S[k].begin(), S[k].end(), X[i])) != S[k].end())
                        {
                            S[k].erase(it);
                        }
                    }
                }
                else
                {
                    ans.push_back(X);
                    k--;
                }
            }
            k--;
        }

        return ans;
    }
};

网上也有用递归的

class Solution {
public:
    vector<vector<int> > permute(vector<int> &num) {
        vector<vector<int> > result;

        permuteRecursive(num, 0, result);
        return result;
    }

    // permute num[begin..end]
    // invariant: num[0..begin-1] have been fixed/permuted
    void permuteRecursive(vector<int> &num, int begin, vector<vector<int> > &result)    {
        if (begin >= num.size()) {
            // one permutation instance
            result.push_back(num);
            return;
        }

        for (int i = begin; i < num.size(); i++) {
            swap(num[begin], num[i]);
            permuteRecursive(num, begin + 1, result);
            // reset
            swap(num[begin], num[i]);
        }
    }
};
时间: 2024-12-06 22:50:21

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

【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】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 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】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】Combinations (middle)

Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For example,If n = 4 and k = 2, a solution is: [ [2,4], [3,4], [2,3], [1,2], [1,3], [1,4], ] 思路:有点像0-1背包问题, 对于从1-n的每一个数字都可以选择放入答案 和不放入答案. 当长度达到k时就是一个符合条件的解. 递归的

【leetcode】Anagrams (middle)

Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be in lower-case. anagrams 的意思是两个词用相同的字母组成  比如 “dog" "god" 思路: 把单词排序 如 dog 按字母排序变为 dgo 用unordered_map<string, int> 记录排序后序列第一次出现时,字符串在输入st

【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

【leetcode】Pascal&#39;s Triangle I &amp; II (middle)

Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] 思路:杨辉三角,直接按规律生成即可 vector<vector<int> > generate(int numRows) { vector<vector<int>>

【LeetCode】 Maximum Subarray

Find the contiguous subarray within an array (containing at least one number) which has the largest sum. For example, given the array [−2,1,−3,4,−1,2,1,−5,4], the contiguous subarray [4,−1,2,1] has the largest sum = 6. More practice: If you have figu