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, 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


题目大意是让寻找下一个排列  ,要求的这个排序与给出的这个排列之间没有其他的排列 

也就是两个排列的前缀要尽可能的长

1,2,4,3-->1,3,4,2.....>中间有 1,3,2,4 排列   可以看出 若要求得下一个排列  应该使得要求的排列与给出的排列有尽可能长的前缀


从后向前遍历  找出第一个 A[i-1] >A[i]的 位置 t

再从后向前遍历  找出第一个 A[j]>A[t]的位置  s

交换 s ---->t   然后 将t后面的位置按从小到大排序 就可以得到下一个排列    ~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值