189. 旋转数组

时间:2019-05-26 20:22:03   收藏:0   阅读:140

旋转数组

给定一个数组,将数组中的元素向右移动 k 个位置,其中 k 是非负数。

说明:
尽可能想出更多的解决方案,至少有三种不同的方法可以解决这个问题。
要求使用空间复杂度为 O(1) 的原地算法。

解法:

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

原文:https://www.cnblogs.com/AimeeCodeWorld/p/10927138.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!