logo头像
Snippet 博客主题

【Leetcode 189】Rotate Array

难度: 中等(Medium)

题目

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].

代码

1
2
3
4
5
6
7
8
9
10
11
12
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.
"""
if nums==None or len(nums)==0 or k<0:
return
t = nums[:]
for i in range(len(t)):
nums[(i+k)%len(t)] = t[i]

评论系统未开启,无法评论!