zoukankan      html  css  js  c++  java
  • 【Next Permutation】cpp

    题目

    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) {
            const int len = num.size();
            if (len<2) return;
            std::vector<int>::iterator pivot=num.end();
            // find the pivot pointer
            for (std::vector<int>::iterator i = num.end()-1; i != num.begin(); --i)
            {
                if ( *i>*(i-1) )
                {
                    pivot = i-1;
                    break;
                }
            }
            // if the largest, directly reverse
            if ( pivot==num.end() ) 
            {
                std::reverse(num.begin(), num.end());
                return;
            }
            // else exchange the value of pivot and it's next larger element
            std::vector<int>::iterator next_larger_pivot = pivot;
            for (std::vector<int>::iterator i = num.end()-1; i != num.begin(); --i)
            {
                if ( *pivot<*i )
                {
                    next_larger_pivot = i;
                    break;
                }
            }
            std::swap(*pivot, *next_larger_pivot);
            // reverse the pivot's right elements
            std::reverse(pivot+1, num.end());
        }
    }

    Tips:

    上网搜搜Next Permutation的算法,分四步:

    1. 从右往左 找左比右小的左 记为pivot

    2. 从右往左 找第一个比pivot大的 记为next_larger_pivot

    3. 交换pivot与next_larger_pivot所指向的元素

    4. 让pivot右边的元素(不包括pivot)逆序

    按照上面的算法,多测测case,找找边界条件。

    注意找到pivot和next_larger_pivot之后要break退出循环。

    ===================================

    第二次过这道题,思路已经记得比较模糊了。能做到的就是捡一遍思路,coding的过程很快。

                // from right to left find first violate increase trend
                int pos1 = -1;
                for ( int i=nums.size()-1; i>0; --i )
                {
                    if ( nums[i]>nums[i-1] )
                    {
                        pos1 = i-1;
                        break;
                    }
                }
                // cout << "pos1:" << pos1 << endl;
                if ( pos1==-1 ) 
                {
                    std::reverse(nums.begin(), nums.end());
                    return;
                }
                // from right to left find the first larger than pos1
                int pos2 = nums.size()-1;
                for ( int i=nums.size()-1; i>=0; --i )
                {
                    if ( nums[i]>nums[pos1] )
                    {
                        pos2 = i;
                        break;
                    }
                }
                // cout << "pos2:" << pos2 << endl;
                // swap pos1 pos2
                std::swap(nums[pos1], nums[pos2]);
                // reverse from pos1's right to end
                std::reverse(nums.begin()+pos1+1, nums.end());

    这个算法就是个套路,多扫几遍记住就OK了。

  • 相关阅读:
    git 重命名文件夹
    关于IDEA的Incoming窗口的问题
    mysql替换字段中部分字符串
    IDEA项目改包名改项目名并启动【我】
    IDEA在自带市场安装黑色主题及调整包管理器字体大小【我】
    解决Invalid bound statement (not found)(Mybatis的Mapper绑定问题)
    java中Date日期类型的大小比较
    MyBatis-Plus Wrapper条件构造器查询大全
    idea黑色主题
    python3安装mysqlclient,解决django使用pymysql报错的问题
  • 原文地址:https://www.cnblogs.com/xbf9xbf/p/4450920.html
Copyright © 2011-2022 走看看