轮转数组【数组】

Problem: 189. 轮转数组

思路 & 解题方法

只需要注意这个和普通算法竞赛不一样即可,原地替换要用:nums[:] = nums[length - k:] + nums[:length - k],而不能用nums = nums[length - k:] + nums[:length - k]

复杂度

时间复杂度:

添加时间复杂度, 示例: O ( n ) O(n) O(n)

空间复杂度:

添加空间复杂度, 示例: O ( n ) O(n) O(n)

Code

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

相关推荐

  1. 轮转数组数组

    2024-01-07 09:30:03       68 阅读
  2. Leetcode 189. 轮转数组

    2024-01-07 09:30:03       44 阅读
  3. 【Leetcode-189.轮转数组

    2024-01-07 09:30:03       39 阅读
  4. [leetcode] 189. 轮转数组

    2024-01-07 09:30:03       44 阅读
  5. Leetcode 189. 轮转数组

    2024-01-07 09:30:03       35 阅读
  6. leetcode-189 轮转数组

    2024-01-07 09:30:03       27 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-07 09:30:03       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-07 09:30:03       106 阅读
  3. 在Django里面运行非项目文件

    2024-01-07 09:30:03       87 阅读
  4. Python语言-面向对象

    2024-01-07 09:30:03       96 阅读

热门阅读

  1. js中浅拷贝和深拷贝的区别

    2024-01-07 09:30:03       59 阅读
  2. ARMday9

    ARMday9

    2024-01-07 09:30:03      59 阅读
  3. 掌握 gRPC:从安装到构建第一个C++ 和Python微服务

    2024-01-07 09:30:03       54 阅读
  4. ubuntu/linux 如何在虚拟环境中下载MPI-IS mesh

    2024-01-07 09:30:03       61 阅读