zoukankan      html  css  js  c++  java
  • [leetcode] Next Permutation

    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
     
     

    next_permutation函数实现原理如下:

             在当前序列中,从尾端往前寻找两个相邻元素,前一个记为*i, 后一个记为*ii,并且满足*i < *ii。然后再从尾端寻找另一个元素*j,如果满足*i < *j,即将第i个元素与第j个元素对调,并将第ii个元素之后(包括ii)的所有元素颠倒排序,即求出下一个序列了。

     1 class Solution
     2 
     3 {
     4 
     5 public:
     6 
     7   void nextPermutation(vector<int> &num)
     8 
     9   {
    10 
    11     if(num.size() < 2)
    12 
    13       return;
    14 
    15  
    16 
    17     int i = 0, k = 0;
    18 
    19  
    20 
    21     for(i = num.size() - 2; i>=0; i--)
    22 
    23       if(num[i] < num[i+1])
    24 
    25         break;
    26 
    27  
    28 
    29     for(k = num.size()-1; k>i; k--)
    30 
    31       if(num[k] > num[i])
    32 
    33         break;
    34 
    35  
    36 
    37     if(i>=0)
    38 
    39       swap(num[i], num[k]);
    40 
    41  
    42 
    43     reverse(num.begin()+i+1, num.end());
    44 
    45   }
    46 
    47 };
    扩展当所求序列为当前序列的字典序的前一个序列时:

    prev permutation函数实现原理如下:

             在当前序列中,从尾端往前寻找两个相邻元素,前一个记为*i, 后一个记为*ii,并且满足*i > *ii。然后再从尾端寻找另一个元素*j,如果满足*i > *j,即将第i个元素与第j个元素对调,并将第ii个元素之后(包括ii)的所有元素颠倒排序,即求出上一个序列了。

     1 class Solution
     2 
     3 {
     4 
     5 public:
     6 
     7   void prevPermutation(vector<int> &num)
     8 
     9   {
    10 
    11     if(num.size() < 2)
    12 
    13       return;
    14 
    15  
    16 
    17     int i=0, k=0;
    18 
    19  
    20 
    21     for(i = num.size()-2; i>=0; i--)
    22 
    23       if(num[i] > num[i+1])
    24 
    25         break;
    26 
    27  
    28 
    29     for(k = num.size()-1; k>i; k--)
    30 
    31       if(num[i] > num[k])
    32 
    33         break;
    34 
    35  
    36 
    37     if(i>=0)
    38 
    39       swap(num[i], num[k]);
    40 
    41  
    42 
    43     reverse(num.begin()+i+1, num.end());
    44 
    45   }
    46 
    47 };
  • 相关阅读:
    Codeforces Round #359 (Div. 2) C. Robbers' watch 鸽巢+stl
    poj 3616 Milking Time dp
    UVA 11401
    UVA 11806 Cheerleaders dp+容斥
    Educational Codeforces Round 4 D. The Union of k-Segments 排序
    Codeforces Round #337 (Div. 2) C. Harmony Analysis 数学
    Codeforces Round #337 (Div. 2) B. Vika and Squares 水题
    Codeforces Round #337 (Div. 2) A. Pasha and Stick 水题
    POJ2402/UVA 12050 Palindrome Numbers 数学思维
    UVA 11076 Add Again 计算对答案的贡献+组合数学
  • 原文地址:https://www.cnblogs.com/lxd2502/p/4398179.html
Copyright © 2011-2022 走看看