Two Pointers Summary

背向双指针

  • Longest Palindromic Substring的中心线枚举算法
  • Find K Closest Elements

相向双指针

两根指针一头一尾,向中间靠拢直到相遇: while left < right

time complexity: O(n)

  1. Reverse
    1. Recover rotated sorted array & rotate string(三步翻转法)
    2. Valid Palindrome
      1. follow up: 可以删掉一个字符的Valid Palindrome(证明题)
  2. Two Sum
  3. Partition

同向双指针

两根指针一前一后,直到前面的指针走过头

  • Sliding Window
  • Fast & Slow Pointers

time complexity: O(n)

  1. Move Zeros Before solving the problem, you need to consider:
    • Does the number of zeros matter?
    • Should the result be in order or not?
    • How to minimize the times of writing zeros in the array?

Two Sum

Fast and Slow Pointers

Quick Sort

Merge Sort