Python 60ms solution


  • 0
    K
    class Solution(object):
    def nextPermutation(self, nums):
        """
        :type nums: List[int]
        :rtype: void Do not return anything, modify nums in-place instead.
        """
        tag = False
        for i in xrange(len(nums)-1, 0, -1):
        	if nums[i-1] < nums[i]:
        		index = 0
        		for j in xrange(i, len(nums)):
        			if nums[j] >nums[i-1]:
        				if j == len(nums) - 1:
        					index = j
        					break
        				else:
        					continue
        			else:
        				index = j - 1
        				break
        		tmp = nums[index]
        		nums[index] = nums[i-1]
        		nums[i-1] = tmp
        		self.quick_sort(nums, i, len(nums)-1)
        		tag = True
        		break
    
        if tag == False:
        	nums.sort()
    
    def quick_sort(self, nums, s, e):
    	if s < e:
    		index = self.sub_sort(nums, s, e)
    		self.quick_sort(nums, s, index)
    		self.quick_sort(nums, index+1, e)
    
    def sub_sort(self, nums, s, e):
    	key = nums[s]
    	while s < e:
    		while s < e and nums[e] >= key:
    			e -= 1
    		while s < e and nums[e] < key:
    			nums[s] = nums[e]
    			s += 1
    			nums[e] = nums[s]
    	nums[e] = key
    	return e

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.