링크
다음 사전 순서 배열 찾기
def nextPermutation(self, nums: List[int]) -> None:
"""
Do not return anything, modify nums in-place instead.
"""
n = len(nums)
if n == 1:
return
for i in range(n - 1, -1, -1):
if nums[i] > nums[i - 1]:
if i == 0: ## ex. 54321
nums.sort()
return
arr = sorted(nums[i:])
idx = bisect.bisect_right(arr, nums[i - 1])
nums[i - 1], arr[idx] = arr[idx], nums[i - 1]
nums[i:] = arr
return
return res
def nextPermutation(self, nums: List[int]) -> None:
"""
Do not return anything, modify nums in-place instead.
"""
n = len(nums)
k = j = n - 1
while k > 0 and nums[k] <= nums[k - 1]:
k -= 1
if k == 0:
nums.sort()
return
k -= 1
while nums[j] <= nums[k]:
j -= 1
nums[k], nums[j] = nums[j], nums[k]
l, r = k + 1, n - 1
while l < r:
nums[l], nums[r] = nums[r], nums[l]
l += 1
r -= 1