leetcode - 189. Rotate Array

Description

Given an integer array nums, rotate the array to the right by k steps, where k is non-negative.

Example 1:

Input: nums = [1,2,3,4,5,6,7], k = 3
Output: [5,6,7,1,2,3,4]
Explanation:
rotate 1 steps to the right: [7,1,2,3,4,5,6]
rotate 2 steps to the right: [6,7,1,2,3,4,5]
rotate 3 steps to the right: [5,6,7,1,2,3,4]

Example 2:

Input: nums = [-1,-100,3,99], k = 2
Output: [3,99,-1,-100]
Explanation: 
rotate 1 steps to the right: [99,-1,-100,3]
rotate 2 steps to the right: [3,99,-1,-100]

Constraints:

1 <= nums.length <= 10^5
-2^31 <= nums[i] <= 2^31 - 1
0 <= k <= 10^5

Follow up:

Try to come up with as many solutions as you can. There are at least three different ways to solve this problem.
Could you do it in-place with O(1) extra space?

Solution

Solved after help…

For a space o ( 1 ) o(1) o(1) solution, one straightforward way is: start with 0, use current value to set the new index, and then update the current value with the original value in the new index, and keep going until we replaced all the numbers.

This works for most cases, but doesn’t work when k = k % n, for example, when n=4 and k=2, if we start with 0, then we jump to 2, then we jump back to 0. This could create a loop.

To avoid this loop, before starting, we should keep track of where we started, and once we found we arrived at start again, we increase start by 1 and keep going.

Time complexity: o ( n ) o(n) o(n)
Space complexity: o ( 1 ) o(1) o(1)

Code

class Solution:
    def rotate(self, nums: List[int], k: int) -> None:
        """
        Do not return anything, modify nums in-place instead.
        """
        k %= len(nums)
        rotate_cnt = 0
        start = 0
        while rotate_cnt < len(nums):
            new_index, value_to_set = start, nums[start]
            while True:
                new_index = (new_index + k) % len(nums)
                ori_value = nums[new_index]
                nums[new_index] = value_to_set
                value_to_set = ori_value
                rotate_cnt += 1
                if new_index == start:
                    break
            start += 1
方法一中使用额外数组的原因在于如果我们直接将每个数字放至它最后的位置,这样被放置位置的元素会被覆盖从而丢失。因此,从另一个角度,我们可以将被替换的元素保存在变量 \textit{temp}temp 中,从而避免了额外数组的开销。 我们从位置 00 开始,最初令 \textit{temp}=\textit{nums}[0]temp=nums[0]。根据规则,位置 00 的元素会放至 (0+k)\bmod n(0+k)modn 的位置,令 x=(0+k)\bmod nx=(0+k)modn,此时交换 \textit{temp}temp 和 \textit{nums}[x]nums[x],完成位置 xx 的更新。然后,我们考察位置 xx,并交换 \textit{temp}temp 和 \textit{nums}[(x+k)\bmod n]nums[(x+k)modn],从而完成下一个位置的更新。不断进行上述过程,直至回到初始位置 00。 容易发现,当回到初始位置 00 时,有些数字可能还没有遍历到,此时我们应该从下一个数字开始重复的过程,可是这个时候怎么才算遍历结束呢?我们不妨先考虑这样一个问题:从 00 开始不断遍历,最终回到起点 00 的过程中,我们遍历了多少个元素? 由于最终回到了起点,故该过程恰好走了整数数量的圈,不妨设为 aa 圈;再设该过程总共遍历了 bb 个元素。因此,我们有 an=bkan=bk,即 anan 一定为 n,kn,k 的公倍数。又因为我们在第一次回到起点时就结束,因此 aa 要尽可能小,故 anan 就是 n,kn,k 的最小公倍数 \text{lcm}(n,k)lcm(n,k),因此 bb 就为 \text{lcm}(n,k)/klcm(n,k)/k。 这说明单次遍历会访问到 \text{lcm}(n,k)/klcm(n,k)/k 个元素。为了访问到所有的元素,我们需要进行遍历的次数为 作者:LeetCode-Solution 链接:https://leetcode.cn/problems/rotate-array/solution/xuan-zhuan-shu-zu-by-leetcode-solution-nipk/ 来源:力扣(LeetCode) 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
02-08
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值