当前位置:网站首页>51. numerical arrangement

51. numerical arrangement

2022-06-23 02:23:00 Twilight lattice

Catalog

One 、 Problem description

Two 、 Problem solving

1. Code


One 、 Problem description

Enter a set of numbers ( May contain duplicate numbers ), Output all its permutations .

Data range

Enter the array length [0,6].

Examples

 Input :[1,2,3]

 Output :
      [
        [1,2,3],
        [1,3,2],
        [2,1,3],
        [2,3,1],
        [3,1,2],
        [3,2,1]
      ]

Two 、 Problem solving

1. Code

class Solution {
public:
    vector<vector<int>> permutation(vector<int>& nums) {
        sort(nums.begin(),nums.end());
        vector<vector<int>> res;
        do
        {
            res.push_back(nums);
        }while(next_permutation(nums.begin(),nums.end()));
        
        return res;
    }
};

next_permutation()

Rearrange elements in scope [first,last) For the next Dictionary order Bigger array . Permutation is every possible permutation of elements ( among N Is the number of elements in the range ). According to them Dictionaries Sort different permutations by comparing them with each other ; The first possible arrangement in this order ( Press The dictionary order is smaller than Permutations of all other permutations ) Is an arrangement of all elements in ascending order , The largest arrangement is that all elements are arranged in descending order .  The comparison of individual elements is done using one of the following methods N! 

原网站

版权声明
本文为[Twilight lattice]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202211744194924.html