zoukankan      html  css  js  c++  java
  • Next Permutation

    Problem Statement

    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

     

    The hard point of this problem is the definition of next permutation. The description of how to generate next permutation is an algorithm to solve this problem.

    From wikipedia, there's a simple algorithm. But we need to give some remarks to analyze it.

    1. Find the largest index $k$ such that $a[k] < a[k + 1]$. If no such index exists, the permutation is the last permutation.
    2. Find the largest index $l$ greater than $k$ such that $a[k] < a[l]$.
    3. Swap the value of $a[k]$ with that of $a[l]$.
    4. Reverse the sequence from $a[k + 1]$ up to and including the final element $a[n]$.

    The reason why we search from right to left, because that's the first place to decrease the value of permutation.

     

    If we get to the beginning, that means what we deal is the minimum permutation.

     

    Otherwise, if we get the largest $k$ such that $a[k] < a[k+1]$, that means:

    • $a[k+1]$ to $a[n]$ is in decreasing order
    • $a[k]$ is also one element in the range from $a[n]$ to $a[k]$.

    These mean we've got the last permutation when we fix {$a[1], a[2], ..., a[k]$}.

    So the next permutation should begin with {$a[1], a[2], ..., a[k-1], a[k']$}, where $a[k']$ is the next larger element than $a[k]$. After that, we start our permutation from $$a[1], a[2], ..., a[k-1], a[k'], a[k+2], ..., a[n]$$, where $$a[1], a[2], ..., a[k-1], a[k']$$ is in increasing order.

    So, in order to get the next permutation, first we need to find the next larger element than $a[k]$. The method we use is step 2:

    Find the largest index $l$ greater than $k$ such that $a[k] < a[l]$.

    Then, we need to swap the two elements $a[k]$ and $a[l]$. Now:

    1. the next permutation's begining part {$a[1], a[2], ..., a[k]$} has been reached right places.
    2. the new $a[l]$ is just less than $a[l-1]$ and greater than $a[l+1]$. So, {$a[k+1], ..., a[n]$} is in decreasing order.

    So, the last step, we need to reverse the remaining parts, {$a[k+1], ..., a[n]$}, to get the right next permutation.

     


    The complete code is: 

    class Solution {
        void swap(vector<int> &num, int i , int j){
            if (i == j) return;
            
            int tmp = num[i];
            num[i] = num[j];
            num[j] = tmp;
        }
        
        void reverse(vector<int> &num, int start, int end){
            if(start >= end) return;
            
            while(start < end){
                swap(num, start, end);
                start++;
                end--;
            }
        }
        
    public:
        void nextPermutation(vector<int> &num) {
            int n = (int)num.size();
            
            if(1 == n) return;
            
            int p = 0, l = 0;
            for(int i = n - 1; i > 0; --i){
                if(num[i] > num[i-1]){
                    p = i - 1;
                    l = i;
                    
                    while(num[l] > num[p] && l < n) ++l;
                    
                    --l;
                    swap(num, p, l);
                    reverse(num, p+1, n-1);
                    return;
                }
            }
            
            reverse(num, 0, n-1);
        }
    };
    
  • 相关阅读:
    改变UITabbar顶部分割线颜色
    UITableViewCell添加点击时改变字体的颜色、背景、图标
    【转】有了Auto Layout,为什么你还是害怕写UITabelView的自适应布局?
    AFNetworking https自签名证书 -1012 解决方案
    关于AFNetWorking 2.5.4之后版本编译报错问题解决方案
    UIImageView 使图片圆形的方法
    关于使用IQKeyBoardManager键盘还是被遮挡的问题解决方案
    关于ios7 以上版本 view被导航栏遮挡的问题 解决方案
    手动导入第三方工程/类库
    “请不要直接访问超全局$_GET数组”
  • 原文地址:https://www.cnblogs.com/kid551/p/4113273.html
Copyright © 2011-2022 走看看