• 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 replacement must be in-place, do not allocate extra memory.

    Here are some examples. Inputs are in the left-hand column and its corresponding outputs are in the right-hand column.
    1,2,3 → 1,3,2
    3,2,1 → 1,2,3
    1,1,5 → 1,5,1

    class Solution {
    public:
        void nextPermutation(vector<int> &num) 
        {
            for(int i=num.size()-1;i>0;i--)
                if(num[i-1]<num[i])
                {
                    int index=i;
                    while(index<num.size() && num[index]>num[i-1]) index++;
                    int tmp=num[i-1];
                    num[i-1]=num[index-1];
                    num[index-1]=tmp;
                    sort(num,i,num.size()-1);
                    return;
                }
            sort(num,0,num.size()-1);
        }
        void sort(vector<int>& num,int l,int r)
        {
            for(int i=l;i<r;i++)
                for(int j=i+1;j<=r;j++)
                    if(num[i]>num[j])
                    {
                        int tmp=num[i];
                        num[i]=num[j];
                        num[j]=tmp;
                    }
        }
    }; 
  • 相关阅读:
    水洼,八连杀
    友链
    万能转换字符类型到int ,int到string,string到char or char *等等
    蓝桥杯模拟赛题
    2020 03 21
    2019 12 02 reading
    CentOS 7 定时计划任务设置
    xinted &telnet
    2019 12 02 section C one
    【暖*墟】#洛谷网课1.30# 树上问题
  • 原文地址:https://www.cnblogs.com/erictanghu/p/3759318.html
Copyright © 2020-2023  润新知