Algorithm for brushing questions: fast and slow pointer method

The fast and slow pointer method refers to the operation of arrays, linked lists, and strings using two pointers with the same starting point but different advance steps. Compared with using multiple loops to solve problems, the fast and slow pointer method has lower time complexity and higher execution efficiency. For the fast and slow pointer method, there are nothing more than two points that can be adjusted according to the topic:

  1. starting point
  2. Step forward

The choice of the starting position of the fast and slow pointer method is usually judged by an if else statement, and when the correct starting position is not reached, the forward steps of the two pointers will remain the same. The method to realize the inconsistent movement of the fast and slow pointers is usually to use a for loop to move the pointers, and pay attention to the problem of crossing the boundary. There are two options for the for loop iteration here:

  1. You can set the number of steps of the fast and slow pointers to be the same, and then adjust the number of steps after the if judgment is successful.
  2. It is also possible to only set the iteration of the fast pointer, and then set the iteration of the slow pointer in the if. If the judgment fails, the slow pointer will not advance, so as to realize the adjustment of the number of steps between the two.

After knowing the framework of the basic fast and slow pointers, that is, the values ​​of the front double pointers are consistent, and then use a for loop to move the fast and slow pointers at the same time before reaching the correct starting position, and the number of steps is the same. At the starting point, just adjust the forward step of the fast pointer to be greater than that of the slow pointer. We will practice a simple LeetCode problem in the future.

Array element removal

Topic link: 27. Remove elements

topic description

Given an array nums and a value val, you need to remove all elements whose value is equal to val in place, and return the new length of the removed array.

Don't use extra array space, you have to use only O(1) extra space and modify the input array in-place.

The order of elements can be changed. You don't need to consider elements in the array beyond the new length.

illustrate:

Why is the returned value an integer, but the output answer is an array?

Note that the input array is passed "by reference", which means that modifications to the input array within the function are visible to the caller.

You can imagine the internal operation as follows:

// nums 是以“引用”方式传递的。也就是说,不对实参作任何拷贝
int len = removeElement(nums, val);

// 在函数里修改输入数组对于调用者是可见的。
// 根据你的函数返回的长度, 它会打印出数组中 该长度范围内 的所有元素。
for (int i = 0; i < len; i++) {
    
    
    print(nums[i]);
}

Example:

example one

Input: nums = [3,2,2,3], val = 3
Output: 2, nums = [2,2]
Explanation: The function should return the new length 2, and the first two elements in nums are both 2. You don't need to consider elements in the array beyond the new length. For example, the new length returned by the function is 2, and nums = [2,2,3,3] or nums = [2,2,0,0], will also be considered as the correct answer.

Example two:

Input: nums = [0,1,2,2,3,0,4,2], val = 2
Output: 5, nums = [0,1,4,0,3]
Explanation: The function should return the new length 5, and the first five elements in nums are 0, 1, 3, 0, 4. Note that these five elements can be in any order. You don't need to consider elements in the array beyond the new length.

hint:

  • 0 <= nums.length <= 100
  • 0 <= nums[i] <= 50
  • 0 <= val <= 100

topic analysis

Briefly introduce the topic, enter an array and the target value, and delete the values ​​in the array that meet the target value. Since the array space is continuous, only the elements after deleting the target value can be moved forward one by one to complete the deletion. If you do not use the fast and slow pointer method, but use double-layer for loops to delete violent loops, the corresponding time complexity will be O(n 2 ) , and the execution efficiency is low, but the problem can still be solved. The idea of ​​the fast and slow pointer method is as follows:

  1. Define two pointer variables, both start at 0
  2. Nest a for loop to realize pointer movement and set fast pointer iteration
  3. Judging the starting point of the pointer: if it is not the target value, exchange the element pointed to by the fast pointer and the slow pointer
  4. Complete the array loop and return the slow pointer

The program code is as follows:

class Solution {
    
    
    public int removeElement(int[] nums, int val) {
    
    
        int fastIndex = 0;
        int slowIndex;
        for (slowIndex = 0; fastIndex < nums.length; fastIndex++) {
    
    
            if (nums[fastIndex] != val) {
    
    
                nums[slowIndex] = nums[fastIndex];
                slowIndex++;
            }
        }
        return slowIndex;
    }
}

Ask for likes and forward

おすすめ

転載: blog.csdn.net/yumuing/article/details/122954343