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

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/jiang425776024/article/details/88562547
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]

猜你喜欢

转载自blog.csdn.net/jiang425776024/article/details/88562547
0条评论
添加一条新回复
  
今日推荐