首页 > 其他 > 详细

LeetCode - Next Permutation

时间:2018-01-04 13:32:12      阅读:276      评论:0      收藏:0      [点我收藏+]

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, do not allocate 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,3 → 1,3,2
3,2,1 → 1,2,3
1,1,5 → 1,5,1

=======================================

My own solve:

class Solution(object):
    def nextPermutation(self, nums):
        """
        :type nums: List[int]
        :rtype: void Do not return anything, modify nums in-place instead.
        """
        l0 = len(nums)
        if l0==2:
            if nums[1]>nums[0]:
                temp = nums[0]
                nums[0]=nums[1]
                nums[1]=temp
        elif l0>2:
            l1 = l0-3
            flag = 0
            while l1>=0:
                k=0
                while (l0-k-l1)>=2:
                    (arr,i) = self.subSort(nums[l1:l0-k])
                    if arr != []:
                        nums[l1:l0-k] = arr
                        ss = nums[l1+i+1:]
                        ss.sort()
                        nums[l1+i+1:]=ss
                        flag = 1
                        break
                    k += 1
                if flag == 1:
                    break
                l1 -= 1
            if flag == 0:
                nums.sort()
            
    def subSort(self, arr):
        l = len(arr)
        while l>=2:
            l1 = l
            while l1>= 1:
                if arr[l-1] > arr[l1-1]:
                    temp = arr[l1-1]
                    arr[l1-1] = arr[l-1]
                    arr[l-1] = temp
                    return(arr,l1-1)
                l1 -= 1 
            l -= 1
        return ([],0)

Others(http://bookshadow.com/weblog/2016/09/09/leetcode-next-permutation/):

解题思路:

首先从右向左遍历数组,找到第一个相邻的左<右的数对,记右下标为x,则左下标为x - 1

若x > 0,则再次从右向左遍历数组,直到找到第一个大于nums[x - 1]的数字为止,记其下标为y,交换nums[x - 1], nums[y]

最后将nums[x]及其右边的元素就地逆置

class Solution(object):
    def nextPermutation(self, nums):
        """
        :type nums: List[int]
        :rtype: void Do not return anything, modify nums in-place instead.
        """
        size = len(nums)
        for x in range(size - 1, -1, -1):
            if nums[x - 1] < nums[x]:
                break
        if x > 0:
            for y in range(size - 1, -1, -1):
                if nums[y] > nums[x - 1]:
                    nums[x - 1], nums[y] = nums[y], nums[x - 1]
                    break
        for z in range((size - x) / 2):
            nums[x + z], nums[size - z - 1] = nums[size - z - 1], nums[x + z]

 

LeetCode - Next Permutation

原文:https://www.cnblogs.com/ywu24/p/8192614.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!