Skip to content

Instantly share code, notes, and snippets.

@cashlo
Last active July 28, 2018 21:31
Show Gist options
  • Save cashlo/31f64b99892a6bd4fa8d4eced333ee43 to your computer and use it in GitHub Desktop.
Save cashlo/31f64b99892a6bd4fa8d4eced333ee43 to your computer and use it in GitHub Desktop.
class Solution(object):
def nextPermutation(self, nums):
"""
:type nums: List[int]
:rtype: void Do not return anything, modify nums in-place instead.
"""
if len(nums) == 1: return
for i in range(len(nums)-2, -1, -1):
if nums[i] < nums[i+1]:
m = min((n, -(j+i+1)) for j,n in enumerate(nums[i+1:]) if n > nums[i])
nums[i], nums[-m[1]] = nums[-m[1]], nums[i]
nums[i+1:] = nums[len(nums)-1:i:-1]
return
nums.sort()
return
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment