当前位置:网站首页>next_ Permutation full permutation function

next_ Permutation full permutation function

2022-06-24 18:34:00 One star accompanies the moon

First of all, the header of this function is in the file < algorithm >
next_permutation() It is arranged in dictionary order , And it increases from the current dictionary order in the array to the maximum dictionary order
Reference link
therefore , This function is used only for ordered arrays
Take the topic of force deduction as an example : The first K Sort by

class Solution {
    
public:
    string getPermutation(int n, int k) {
    
        if(!n) return "";
        vector<char> ch;
        for(int i=0;i<n;i++) ch.push_back(i+1+'0');
        int i=0;
        do{
    
            if(++i==k) break;
        }while(next_permutation(ch.begin(),ch.end()));
        string str;
        for(auto i : ch){
    
            str+=i;
        }
        return str;
    }
};
原网站

版权声明
本文为[One star accompanies the moon]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202211333580569.html