https://leetcode.com/problems/rotate-array/
Rotate an array of n elements to the right by k steps.
For example, with n = 7 and k = 3, the array[1,2,3,4,5,6,7]
is rotated to[5,6,7,1,2,3,4]
.Note:
Try to come up as many solutions as you can, there are at least 3 different ways to solve this problem.
向右轮转一个有n个元素的数组k步。比如:n = 7,k = 3时,数组 [1,2,3,4,5,6,7]
可以得到 [5,6,7,1,2,3,4]
。
注:尽量用多种方法来解决这个问题。
(另外:注意只能直接修改原数组,不能返回新的数组,修改必须是”in-place”的。)
利用Python的数组slice的操作和赋值,实际上相当于创建了两个新的数组,分别保存原数组的前后部分,再依次将元素赋值给原数组。【时间复杂度O(n),空间复杂度O(n)】
代码
class Solution(object):
def rotate(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: void Do not return anything, modify nums in-place instead.
"""
k = k % len(nums)
nums[:k], nums[k:] = nums[len(nums)-k:], nums[:len(nums)-k]
只创建一个原数组的拷贝,然后根据循环的公式对原数组进行重新赋值。【时间复杂度O(n),空间复杂度O(n)】
代码
class Solution(object):
def rotate(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: void Do not return anything, modify nums in-place instead.
"""
old_nums = nums[:]
for i in xrange(len(nums)):
nums[(i + k) % len(nums)] = old_nums[i]
将前n-k个原地反转,将后k个原地反转,再将整个数组原地反转,即得所求。【时间复杂度O(n),空间复杂度O(1)】
代码
class Solution(object):
def rotate(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: void Do not return anything, modify nums in-place instead.
"""
k = k % len(nums)
self.reversePart(nums, 0, len(nums)-k-1)
self.reversePart(nums, len(nums)-k, len(nums)-1)
self.reversePart(nums, 0, len(nums)-1)
def reversePart(self, nums, start, end):
while start < end:
nums[start], nums[end] = nums[end], nums[start]
start, end = start+1, end-1
实际上,为了使空间复杂度为O(1),我们可能会想到对原数组做k次“循环右移一位”的解法。然而直接这么做会超时,所以可以考虑下面的优化做法。其思想是,每次把数组最后k位交换到正确的位置,循环直到所有元素位置正确。
代码
class Solution(object):
def rotate(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: void Do not return anything, modify nums in-place instead.
"""
k, start, n = k % len(nums), 0, len(nums)
while k % n != 0 and n > 0:
for i in xrange(k):
nums[start + i], nums[len(nums) - k + i] = nums[len(nums) - k + i], nums[start + i]
start, n = start + k, n - k
k = k % n
PS: 新手刷LeetCode,新手写博客,写错了或者写的不清楚还请帮忙指出,谢谢!
转载请注明:http://blog.csdn.net/coder_orz/article/details/52052767