3.旋转数组(Python)

版权声明:@author:geek_aaron https://blog.csdn.net/weixin_39433783/article/details/82985124

在这里插入图片描述
思路
利用数组切片的思想

参考代码

class Solution:
    def rotate(self, nums, k):
        """
        :type nums: List[int]
        :type k: int
        :rtype: void Do not return anything, modify nums in-place instead.
        """
        mod = k%len(nums)           #移动位数对数组长度取余数
        nums[:]=nums[-mod:]+nums[:-mod] #利用数组切片
        

猜你喜欢

转载自blog.csdn.net/weixin_39433783/article/details/82985124