恢复旋转排序数组

给定一个旋转排序数组,在原地恢复其排序。

说明

什么是旋转数组?

  • 比如,原始数组为[1,2,3,4], 则其旋转数组可以是[1,2,3,4], [2,3,4,1], [3,4,1,2], [4,1,2,3]
样例

[4, 5, 1, 2, 3] -> [1, 2, 3, 4, 5]

挑战 

使用O(1)的额外空间和O(n)时间复杂度

用到的知识点 》》》》》数组的排序,

class Solution:
"""
@param: nums: An integer
@return:
"""
def recoverRotatedSortedArray(self, nums):
  # write your code here
  # 首先先找到最小的1,通过一次循环找到比nunms[0]要小的,肯定是最小的数
  n=len(nums)
  for i in range(n):
    if nums[i]<nums[0]:
      min=i

       #然后在将拿到的索引,之前的全部append到nums后面,再讲之前的删除      

      for j in range(min):
        nums.append(nums[j])
      del nums[0:min]

 

原文地址:https://www.cnblogs.com/KK150713/p/7425006.html