Abstract


  • Mainly used to solve Array, OOP & Linked List related problems
  • Reduce time complexity by one dimension, O(n^2) to O(n)

Fast-Slow Pointers


Leetcode Questions

Left-Right Pointers


// Java
 
public void twoPointerSort(int left, int right, int[] arr) {
    int temp;
    while (left < right) {
        temp = arr[right];
        arr[right--] = arr[left];
        arr[left++] = temp;
    }
}

Leetcode Questions

Sliding Window


  • Two Pointers (双指针) move in the same direction at the same or different speed
  • Can be used to find Minimum Size Subarray that the sum of the elements is equal or greater than the given target value
  • 3 points to consider
    1. What should be inside the Window?
    2. When should we increment the left pointer?
    3. When should we increment the right pointer?

Leetcode Questions

Terminologies


Window

  • The elements within the left and right pointers when we are performing Sliding Window
  • The operation of expanding and shrinking the window is usually the same