【leetcode_medium】31. Next Permutation

leetcode_medium_array

problem

31. Next Permutation

solution #1: 

According to Wikipedia, a man named Narayana Pandita presented the following simple algorithm to solve this problem in the 14th century.

Find the largest index k such that nums[k] < nums[k + 1]. If no such index exists, just reverse nums and done.
Find the largest index l > k such that nums[k] < nums[l].
Swap nums[k] and nums[l].
Reverse the sub-array nums[k + 1:].

code

 

 

参考

1. leetcode_31. Next Permutation;

2. [高中生也能看懂的算法逻辑1] 下一个排列

posted on 2020-10-27 22:59  鹅要长大  阅读(113)  评论(0编辑  收藏  举报

导航