题目

LeetCode旋转数组(Python)

解题思路

1、暴力法——每次将所有元素向右移动一位,总共移动k次:

LeetCode旋转数组(Python)
但由于时间复杂度为O(n*k),因此执行结果超出时间限制。

2、环状替换法——官方解释已经比较清楚了,这里放上来:

LeetCode旋转数组(Python)
代码为:

LeetCode旋转数组(Python)
执行结果为:

LeetCode旋转数组(Python)
3、反转法——

LeetCode旋转数组(Python)
代码为:

LeetCode旋转数组(Python)

执行结果为:

LeetCode旋转数组(Python)

相关文章: