【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了。

posted on   承续缘  阅读(200)  评论(0编辑  收藏  举报

编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示