zoukankan      html  css  js  c++  java
  • LeetCode--Next Permutation

    (1)从后往前,找到a[i]<a[i+1]

    (2)从i后面找到最小的比a[i]大的元素,交换(i后面的元素都是递减的,所以实现就看自己的了)

    (3)将i后的元素reverse一下即可

     1 class Solution {
     2 public:
     3     void nextPermutation(vector<int> &num) {
     4         int end = num.size() - 1;
     5         int povit = end;
     6         while(povit > 0){
     7             if(num[povit] > num[povit - 1]) break;
     8             povit --;
     9         }
    10         if(povit > 0){
    11             povit --; 
    12             int large = end;
    13             while(num[large] <= num[povit]) large --;
    14             swap(num[large] , num[povit]);
    15             reverse(num.begin() + povit + 1 , num.end());
    16         }else{
    17             reverse(num.begin() , num.end());
    18         }
    19     }
    20 };
  • 相关阅读:
    mongo常用查询
    MongoDB
    python连接mongo
    linux 硬盘
    kali 日志
    linux 命令
    grep
    linux shell
    linux 匹配字符串是否为数字
    linux 第一题 计算题
  • 原文地址:https://www.cnblogs.com/cane/p/3965150.html
Copyright © 2011-2022 走看看