1. 程式人生 > >31. Next Permutation 返回下一個pumutation序列

31. Next Permutation 返回下一個pumutation序列

class code ges respond 一句話 ranges reverse wiki eight

[抄題]:

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 and use only constant 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,31,3,2
3,2,11,2,3
1,1,51,5,1

[暴力解法]:

時間分析:

空間分析:

[優化後]:

時間分析:

空間分析:

[奇葩輸出條件]:

[奇葩corner case]:

[思維問題]:

完全不知道怎麽操作啊:從後往前找遞增,再把遞減的尾巴整個reverse一遍,變成遞增。

[英文數據結構或算法,為什麽不用別的數據結構或算法]:

[一句話思路]:

把遞減的尾巴整個reverse一遍,變成遞增。i和後面交換,確保更加遞增

[輸入量]:空: 正常情況:特大:特小:程序裏處理到的特殊情況:異常情況(不合法不合理的輸入):

[畫圖]:

技術分享圖片

[一刷]:

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分鐘肉眼debug的結果]:

[總結]:

[復雜度]:Time complexity: O() Space complexity: O()

[算法思想:叠代/遞歸/分治/貪心]:

[關鍵模板化代碼]:

[其他解法]:

[Follow Up]:

[LC給出的題目變變變]:

[代碼風格] :

[是否頭一次寫此類driver funcion的代碼] :

[潛臺詞] :

31. Next Permutation 返回下一個pumutation序列