Advanced usage and examples of double pointer arithmetic

Double pointer is a commonly used arithmetic technique, usually used in the operation of array or linked list. In some cases, we may need to use advanced usage of double pointers to solve the problem. In this article, I'll introduce advanced usage of double pointers and provide some JavaScript sample code to help you understand.

  1. speed pointer

The fast and slow pointer is an advanced usage of double pointer, which is usually used in the operation of the linked list. The fast pointer and the slow pointer traverse from the head of the linked list at the same time, the fast pointer traverses two nodes at a time, and the slow pointer traverses one node at a time. When the fast pointer traverses to the end of the linked list, the node pointed by the slow pointer is the middle node of the linked list.

This method has a wide range of application scenarios, for example, it can be used to determine whether a linked list has a ring, find the intermediate node of the linked list, and so on. Here is a sample code:

class ListNode {
    
    
  constructor(val, next = null) {
    
    
    this.val = val;
    this.next = next;
  }
}

function findMiddleNode(head) {
    
    
  let fast = head;
  let slow = head;

  while (fast !== null && fast.next !== null) {
    
    
    fast = fast.next.next;
    slow = slow.next;
  }

  return slow;
}

const node1 = new ListNode(1);
const node2 = new ListNode(2);
const node3 = new ListNode(3);
const node4 = new ListNode(4);
const node5 = new ListNode(5);

node1.next = node2;
node2.next = node3;
node3.next = node4;
node4.next = node5;

console.log(findMiddleNode(node1)); // Output: {val: 3, next: {val: 4, next: {val: 5, next: null}}}
  1. sliding window

Sliding window is an advanced usage of double pointer, which is usually used in the operation of string or array. A sliding window refers to a fixed-size window that slides from left to right in a string or an array, and the content in the window changes continuously.

Sliding windows can be used to solve some string or array problems, such as finding the longest or shortest substring in a string, etc. Here is a sample code:

function findSubstring(s, t) {
    
    
  const result = [];
  if (!s || !t || s.length < t.length) {
    
    
    return result;
  }

  const map = new Map();
  for (let c of t) {
    
    
    map.set(c, map.has(c) ? map.get(c) + 1 : 1);
  }

  let left = 0,
    right = 0,
    count = t.length;
  while (right < s.length) {
    
    
    const d = s[right];
    if (map.has(d) && map.get(d) > 0) {
    
    
      count--;
    }
    map.set(d, map.get(d) - 1);
    right++;

    if (count === 0) {
    
    
      const leftChar = s[left];
      if (map.has(leftChar) && map.get(leftChar) >= 0) {
    
    
        count++;
      }
      map.set(leftChar, map.get(leftChar) + 1);

      if (right - left === t.length) {
    
    
        result.push(left);
      }
      left++;
    }
  }

  return result;
}

console.log(findSubstring("barfoothefoobarman", "foo")); // Output: [0, 9]

In the sample code above, we use a sliding window to sfind tthe start of a substring in the string that contains all the characters in the string . We use mapto record tthe number of occurrences of each character in the string, and use the leftand rightvariables to record the left and right boundaries of the window, and use countthe variable to record how many characters need to be found in the current window.

When we move the right border, we continuously reduce the number of corresponding characters mapin , if this character mapexists in and the number is greater than 0, it means that we have found a character we need to find, and we countneed to subtract 1. When countis reduced to 0, it means that the current window has contained tall the characters in the string, at this time we move the left border until the window no longer contains all the characters tin , and then continue to move the right border. We keep maintaining this process until the right bound reaches the end sof . Finally, we return the starting positions of all found substrings.

Summarize

Double pointers are a very useful algorithmic trick that can be used to solve many array and linked list problems. In some cases, we may need to use advanced usage of double pointers, such as fast and slow pointers and sliding windows. This article introduces the advanced usage of double pointers, and provides JavaScript sample code, hoping to help you better understand and apply double pointer arithmetic.

Supongo que te gusta

Origin blog.csdn.net/qq_29669259/article/details/130122519
Recomendado
Clasificación