LeetCode - 46. Permutations

简介: 46. Permutations  Problem's Link  ---------------------------------------------------------------------------- Mean:  给定一个数组,求这个数组的全排列.

46. Permutations 

Problem's Link

 ----------------------------------------------------------------------------

Mean: 

给定一个数组,求这个数组的全排列.

analyse:

方法1:调用自带函数next_permutation(_BIter,_BIter)

方法2:自己手写一个permutation函数,很简单.

Time complexity: O(N)

 

view code

#include <bits/stdc++.h>
using namespace std;

// way 1: use system's function:next_permutation(iter* begin,iter* end);
//class Solution
//{
//public:
//    vector<vector<int>> permute(vector<int>& nums)
//    {
//        vector<vector<int>> res;
//        sort(nums.begin(),nums.end());
//        res.push_back(nums);
//        while(next_permutation(nums.begin(),nums.end()))
//            res.push_back(nums);
//        return res;
//    }
//};

class Solution
{
public :
    vector < vector < int >> permute( vector < int > nums)
    {
        vector < vector < int >> res;
        solvePermute( res , nums , 0);
        return res;
    }
   
    void solvePermute( vector < vector < int >>& res , vector < int > nums , int begin)
    {
        if( begin >= nums . size())
        {
            res . push_back( nums);
            return;
        }
        for( int i = begin; i < nums . size(); ++ i)
        {
            swap( nums [ i ], nums [ begin ]);
            solvePermute( res , nums , begin + 1);
            swap( nums [ i ], nums [ begin ]);
        }
    }
};

int main()
{
    int n;
    while( cin >>n)
    {
        vector < int > ve(n);
        for( int i = 0; i <n; ++ i)
            cin >> ve [ i ];
        Solution solution;
        vector < vector < int >> ans = solution . permute( ve);
        for( auto p1: ans)
        {
            for( auto p2: p1)
            {
                cout << p2 << " ";
            }
            cout << endl;
        }
    }
    return 0;
}
目录
相关文章
|
人工智能
LeetCode 47. Permutations II
给定一组可能有重复元素不同的整数,返回所有可能的排列(不能包含重复)。
72 0
LeetCode 47. Permutations II
LeetCode 46. Permutations
给定一组不同的整数,返回所有可能的排列。
52 0
|
算法
[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],
1051 0
[LeetCode]--46. Permutations
Given a collection of distinct 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],
1237 0
LeetCode - 47. Permutations II
47. Permutations II  Problem's Link  ---------------------------------------------------------------------------- Mean:  给定一个数组(元素可能重复),求这个数组的全排列.
818 0
[LeetCode] Permutations
Well, have you solved the nextPermutation problem? If so, your code can be used in this problem. The idea is fairly simple: sort nums in ascending o...
896 0
[LeetCode] Permutations II
Well, have you solved the nextPermutation problem? If so and you have handled the cases of duplicates at that problem, your code can be used in this problem.
696 0