数字政务小程序如何提升政务服务效率与公众参与度?
1005
2022-08-25
LeetCode-31. Next Permutation
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
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,23,2,1 → 1,2,31,1,5 → 1,5,1
题解:
从后往前找,找到第一个逆序对,即nums[p + 1] < nums[p],然后再次从后往前找,找到第一个比p大的数下标q,交换nums[p]和nums[q],之后再把p后面的元素升序排列获取最小值。
class Solution {public: void swap(int &a, int &b) { int idx = a; a = b; b = idx; } void nextPermutation(vector
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~