数组-(Recover Rotated Sorted Array)恢复被旋转的排序数组

class Solution:
    """
    @param nums: The rotated sorted array
    @return: nothing
    """

    def recoverRotatedSortedArray(self, nums):
        # write your code here
        if nums == None:
            return
        numslen = len(nums)
        for index in range(numslen - 1):
            # 找到旋转的那个点
            if (nums[index] > nums[index + 1]):
                self.reverse(nums, 0, index)
                self.reverse(nums, index + 1, numslen - 1)
                self.reverse(nums, 0, numslen - 1)

    def reverse(self, nums, start, end):
        # 无条件反转
        while start < end:
            nums[start], nums[end] = nums[end], nums[start]
            start += 1
            end -= 1


s = Solution()
nums = [4, 5, 1, 2, 3]
s.recoverRotatedSortedArray(nums)
print(nums)

5,4,1,2,3

5,4,3,2,1

1,2,3,4,5

[1, 2, 3, 4, 5]

posted @ 2019-03-14 21:59  jj千寻  阅读(84)  评论(0编辑  收藏  举报