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.
- Find the largest index such that . If no such index exists, the permutation is the last permutation.
- Find the largest index greater than such that .
- Swap the value of with that of .
- Reverse the sequence from up to and including the final element .
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 such that , that means:
- to is in decreasing order
- is also one element in the range from to .
These mean we've got the last permutation when we fix {}.
So the next permutation should begin with {}, where is the next larger element than . After that, we start our permutation from , where is in increasing order.
So, in order to get the next permutation, first we need to find the next larger element than . The method we use is step 2:
Find the largest index greater than such that .
Then, we need to swap the two elements and . Now:
- the next permutation's begining part {} has been reached right places.
- the new is just less than and greater than . So, {} is in decreasing order.
So, the last step, we need to reverse the remaining parts, {}, 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); } }; |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 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)