旋转数组(189)

法一:
class Solution:
    def rotate(self, nums: List[int], k: int) -> None:
        """
        Do not return anything, modify nums in-place instead.
        """
        n = len(nums)
        k %= n
        nums[:] = nums[n-k:]+nums[:n-k]
法二:
class Solution:
    def rotate(self, nums: List[int], k: int) -> None:
        """
        Do not return anything, modify nums in-place instead.
        """

      n = len(nums)
      k %= n
      nums[:] = nums[::-1]
      nums[:k] = nums[:k][::-1]
      nums[k:] = nums[k:][::-1]

法三:

class Solution:
    def rotate(self, nums: List[int], k: int) -> None:
        """
        Do not return anything, modify nums in-place instead.
        """

      n = len(nums)
      k %= n
      for _ in range(k):
      nums.insert(0, nums.pop())

 

 

 

 

posted on 2020-08-03 10:39  不要挡着我晒太阳  阅读(112)  评论(0编辑  收藏  举报

导航