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[k1],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[k1],a[k],a[k+2],...,a[n], where a[1],a[2],...,a[k1],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[l1] 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: 

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
32
33
34
35
36
37
38
39
40
41
42
43
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);
    }
};

 

posted @   kid551  阅读(168)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· winform 绘制太阳,地球,月球 运作规律
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示