题解 | #旋转数组#
旋转数组
https://www.nowcoder.com/practice/e19927a8fd5d477794dac67096862042
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
# 旋转数组
# @param n int整型 数组长度
# @param m int整型 右移距离
# @param a int整型一维数组 给定数组
# @return int整型一维数组
#
class Solution:
def solve(self , n: int, m: int, a: List[int]) -> List[int]:
move = m % n
# [1,2,3,4,5,6] --> [6,5,4,3,2,1]
a = self.reverse(a, 0, len(a) - 1)
# [6,5,4,3,2,1] --> [5,6,4,3,2,1]
a = self.reverse(a, 0, move - 1)
# [5,6,4,3,2,1] --> [5,6,1,2,3,4]
a = self.reverse(a, move, len(a) - 1)
return a
# 用于对列表start到end部分进行逆序。
def reverse(self, lst, start, end):
# 用首尾双指针反转列表指定部分。
while end - start >= 1:
temp = lst[start]
lst[start] = lst[end]
lst[end] = temp
start += 1
end -= 1
return lst

顺丰集团工作强度 274人发布