題目描述:
實現(xiàn)獲取下一個排列的函數(shù),算法需要將給定數(shù)字序列重新排列成字典序中下一個更大的排列。
如果不存在下一個更大的排列,則將數(shù)字重新排列成最小的排列(即升序排列)。
必須原地修改,只允許使用額外常數(shù)空間。
以下是一些例子,輸入位于左側(cè)列,其相應輸出位于右側(cè)列。
1,2,3 → 1,3,2 3,2,1 → 1,2,3 1,1,5 → 1,5,1
代碼:
class Solution(object):
def nextPermutation(self, nums):
"""
:type nums: List[int]
:rtype: None Do not return anything, modify nums in-place instead.
"""
is_possible = False
# step 1: find the first nums[i] < nums[ii] from the tail
for i in range(len(nums)-1, 0, -1):
if nums[i-1] < nums[i]:
i, ii = i-1, i
is_possible = True
break
# step 2: find the first nums[j] > nums[i] from the tail and swap nums[i], nums[j]
if is_possible:
for j in range(len(nums)-1, -1, -1):
if nums[j] > nums[i]:
nums[i], nums[j] = nums[j], nums[i]
# step 3: reverse all the elements starting from ii
nums[ii:] = nums[ii:][::-1]
break
else:
nums[:] = nums[::-1]
什么是下一個排列以及本題的思路分析:
核心思想是:
- step 1:首先從最尾端開始往前尋找兩個相鄰元素,令第一元素為
i,第二個元素為ii,且滿足nums[i] < nums[ii]; - step 2:找到這樣一組相鄰元素后,再從最尾端開始往前檢驗,找出第一個大于
i的元素,令為j,將nums[i]和nums[j]對調(diào); - step 3:再將
nums[ii:]反轉(zhuǎn)(reverse)。
曾出錯過的地方:
- 題目中明確說明不能返回任何東西,所以最開始寫代碼時,發(fā)現(xiàn)在VScode中能夠通過,但是在LeetCode中總是報錯,而且在LeetCode中的輸出和在VScode中的輸出不一致。后來發(fā)現(xiàn)是自己在代碼中寫了
return nums語句,從而導致出錯; -
range是 左閉右開 的,在上述代碼中用到了兩處range,第一處是range(len(nums)-1, 0, -1),這里右邊只能取到 1,是無法取到 0 的(第二處的range(len(nums)-1, -1, -1)只能取到 0 而無法取到 -1),這一點在寫代碼時必須非常明確,否則非常容易出錯。 - 最后的
else語句不能寫成nums = nums[::-1]。