Punch card question nine

#左耳音风 ARST check-in activity restart#

Table of contents

 1. Problems

 2. Problem solving method one

 3. Problem solving method 2

Fourth, the difference between the two methods


Interpretation of ARTS - Complete one ARTS per week:
● Algorithm: Do at least one LeetCode algorithm problem per week
● Review: Read and comment on at least one English technical article
● Tips: Learn at least one technical skill
● Share: Share one Technical articles with opinions and thoughts

It is hoped that through this event, a wave of people who love technology can be gathered, and the spirit of curiosity, exploration, practice, and sharing can be continued.
 


 1. Problems

Given an integer array nums, rotate the elements of the array k positions to the right, where k is a non-negative number. Example 1: Input: nums = [1,2,3,4,5,6,7], k = 3 Output: [5,6,7,1,2,3,4] Explanation: Turn right by 1 step : [7,1,2,3,4,5,6] Turn right 2 steps: [6,7,1,2,3,4,5] Turn right 3 steps: [5,6,7, 1,2,3,4] Example 2: Input: nums = [-1,-100,3,99], k = 2 Output: [3,99,-1,-100] Explanation: Turn right by 1 step : [99,-1,-100,3] Turn right 2 steps: [3,99,-1,-100]

 2. Problem solving method one

class Solution:
    def rotate(self, nums: List[int], k: int) -> None:
        n = len(nums)
        k %= n
        nums.extend(nums[:k])
        nums[:k] = nums[-k:]

This code implements a function `rotate`, which is used to rotate the given integer array `nums` to the right by `k` positions.

The function has two input parameters:

- An integer array `nums`, representing the array that needs to be rotated.
- A non-negative integer `k`, indicating the number of positions to be rotated to the right. Note that `k` here is modulo `n` (that is, `k % n`), because when `k` is greater than or equal to `n`, only one complete rotation is required.

The main idea of ​​the function is to divide the array into two parts, the first `k` elements and the remaining elements, and then splicing these two parts together to get the rotated array. The specific implementation process is as follows:

1. First calculate the length `n` of the array, and `k % n` the result of modulo `n`. This is because when `k` is greater than or equal to `n`, only one full rotation is required.

2. Then use the `extend()` method of the list to add the first `k` elements at the end of the array. In this way, a new array is obtained, in which the first `k` elements are the first half of the original array, and the remaining elements are the second half of the original array.

3. Finally, use list slicing to move the remaining elements in the new array to the front, and you can get the rotated array. Specifically, we can take the `k`th element to the last element in the new array, and then put them in the corresponding positions in the first half of the new array. This completes the entire rotation operation.

In summary, the time complexity of this code is O(n) and the space complexity is O(1).

 3. Problem solving method 2

Another way to solve the problem is to use the double pointer method. Specifically, we can define two pointers `left` and `right`, which point to the beginning and end of the array respectively. Then do the following:

1. Move the `left` pointer to the right by `k` positions until it points to the `k`th element of the array.
2. Move the `right` pointer `k` positions to the left until it points to the `nk`th element of the array.
3. Swap the two elements pointed by `left` and `right` to complete the rotation operation.

The time complexity of this method is O(n) and the space complexity is O(1).

 

def rotate(nums, k):
    n = len(nums)
    k %= n  # 对 n 取模,防止 k 大于等于 n 的情况
    left, right = 0, n - 1
    for _ in range(k):
        temp = nums[left]
        nums[left] = nums[right]
        nums[right] = temp
        left += 1
        right -= 1

Fourth, the difference between the two methods

The difference between the two methods is that the single-pointer method can only find one element satisfying the condition, while the double-pointer method can find all elements satisfying the condition within the time complexity of O(n). Specifically, the single-pointer method traverses from the beginning of the array, and if an element that meets the condition is found, the position of the element is returned; otherwise, it continues to traverse backwards until the entire array is traversed. The double-pointer method traverses from both ends of the array, moving one pointer at a time, and when the two pointers meet, exchange the positions of the elements they point to. This ensures that each element is visited once. 

 

Guess you like

Origin blog.csdn.net/m0_49914128/article/details/132012804