Python classic data structure and algorithm double pointer collision pointer speed pointer

Double pointer collision pointer 

That is, the two pointers point to the head and the tail respectively, move closer to the middle until they meet, the program terminates, and moves according to certain rules during the movement;

Generally use List or string as data in python

For example, the following program (source Github, the address is the bottom) is used to solve: (Leetcode 167) Address: https://leetcode-cn.com/problems/two-sum-ii-input-array-is-sorted/

Given an ordered array that has been arranged  in ascending order , find two numbers so that their sum is equal to the target number.

The function should return these two subscript values  index1 and index2, where index1 must be less than index2 .

Problem-solving ideas:

Because it is an ordered array, arranged from small to large.

The ending sum corresponds to the middle value, and the head increases backward;

On the contrary, the tail moves forward and becomes smaller;

The smallest sum is index 0 and 1;

The largest sum is the index n-2 n-1, and n is the length of the List;

The end condition is head and tail collision.

class Solution:
    def twoSum(self, numbers: List[int], target: int) -> List[int]:
        if not numbers: return []  #输入列表不存在,返回空值
        start, end = 0, len(numbers)-1  #头尾指针

        while start < end:  #保证尾指针在头指针后边
            _sum = numbers[start] + numbers[end]
            if  _sum < target:  #小于目标值,首指针后移
                start += 1
            elif _sum > target: #大于目标值,尾指针前移
                end -= 1
            else:               #等于目标值,返回结果
                return [start + 1, end + 1]
        return []

Double pointer speed pointer

That is, the pointer has front and back, the one that goes in front is the fast pointer, and the one behind is the slow pointer

For example, the following question: Leetcode 283  https://leetcode-cn.com/problems/move-zeroes/

Given an array  nums, write a function to move everything  0 to the end of the array while maintaining the relative order of non-zero elements.

The source of the program is Github, the address is the lowest;

class Solution:
    def moveZeroes(self, nums: List[int]) -> None:
        """
        Do not return anything, modify nums in-place instead.
        """
        slower, faster = 0, 0 #快指针指向不为零的元素,慢指针指向零元素,满足条件就交换
        while faster < len(nums):
            if nums[slower] != 0:
                slower += 1
            elif nums[faster] != 0:
                nums[slower],  nums[faster] = nums[faster], nums[slower]
                slower += 1
            faster += 1

Problem-solving ideas:

The fast pointer will increase every step until it is an element that is not zero;

When the slow pointer hits a zero element, it is aligned with it;

If the slow pointer is zero and the fast pointer is non-zero, swap, and then the pointers are incremented by one;

The end condition is that the fast pointer is out of bounds, because the fast pointer moves fast;

The above codes are sourced from Github, and their analysis process is recorded.

Reference source: https://github.com/lxztju/leetcode-algorithm

Guess you like

Origin blog.csdn.net/li4692625/article/details/109568266