Two Pointers

167. Two Sum II - Input array is sorted

Use two pointers to move from beginning and end. Until two pointers converge:

  • Move end pointer when the sum of two numbers at the pointers are larger than the target.

  • Move start pointer when the sum is smaller than the target

  • return pointer positions when the sum equals to the target

public int[] twoSum(int[] numbers, int target) {
    int i = 0;
    int j = numbers.length - 1;
    while (i < j) {
        if (numbers[i] + numbers[j] == target) {
            return new int[]{i + 1, j + 1};
        } else if (numbers[i] + numbers[j] < target) {
            i++;
        } else {
            j--;
        }
    }
    return null;
}

Split words by " ". Reverse order by using two pointers technique.

Java:

Python:

Save all vowels to a set. Use two pointers to move from beginning and end. Keep moving the pointer until the pointer encounter a vowel character. Swap vowels.

Use two pointers to move from beginning and end. Keep moving the pointer until the pointer encounter an alphanumeric character. Check if both characters are the same.

Use two pointers to start both at the beginning of array. i to count the numbers of non-zero elements, j to traverse to find none zero elements. When j detects a candidate, swap with i, i++.

Instead of swap, filling the rest of i with 0 also works. This is because i counts the number of non-zeros, the rest would be zeros.

One j pointer to loop over the array, the other pointer stores the right sequence of the result, which satisfy that 2 digits behind was smaller than number at j. If the numbers one those two pointers are the same, it means that there are 3 or more than 3 same numbers, thus move j only until you find the next larger element.

For example:

i j

[1,1,1,2,2,4,5,6,6,6]

This is a situation to replace the number at i with the number at j.

844. Backspace String Compare

Word Consisted by Another Word

Use set to store the shorter array and check on the array.

Use HashMap on 350 to count each element. If sorted. Use two pointers start from beginning.

Follow up:

What if nums1's size is small compared to nums2's size? Which algorithm is better? HashMap

What if elements of nums2 are stored on disk, and the memory is limited such that you cannot load all elements into the memory at once?

  1. Store the two strings in distributed system(whether self designed or not), then using MapReduce technique to solve the problem;

  2. Processing the Strings by chunk, which fits the memory, then deal with each chunk of data at a time;

  3. Processing the Strings by streaming, then check.

Flip 0 to n - k and flip n - k to n, then flip the entire array

217. Contains Duplicate

219. Contains Duplicate II

220. Contains Duplicate III

Use L and R on each k

Since the requirement states "can't use division", we can only generate the result by multiplication. It starts to build an array with all product of the elements before current. For example:

Array = [3,5,1,2], res start withs [1,1,1, 1], then res[i] = res[i - 1] * array[i - 1]

res = [1, 3, 15, 15]

Then multiply from the right, with a start variable as 1, keeps the variable as all the products along the way.

product = 1

  1. res = [ 15 ] then product = 2 * 1 = 2

  2. res = [ . 30,15 ] then product = 1 * 2

  3. res = [ 6, 30,15 ] then product = 5 * 2 = 10

  4. res = [10, 6, 30,15 ]

This method is due to multiply all elements before the current product from left, then multiply all products from the right. It skips the current element.

334. Increasing Triplet Subsequence

This question can be solved by sort, which takes O(nlogn). A better way is to use a slow and fast pointer:

Floyd's Tortoise and Hare
  1. Start with index 0, find the point where two pointers meet. That indicates that there is a cycle in the array.

  2. Move one pointer to the start point. Move both pointers forward in the same speed until they meet again.

  3. Return the index, which is the entrance of the cycle

Refer to Linked List Cycle II

169. Majority Element

229. Majority Element II

Trapping Rain Water

41. First Missing Positive

245. Shortest Word Distance III

611. Valid Triangle Number

55. Jump Game

45. Jump Game II

274. H-Index

275. H-Index II

128. Longest Consecutive Sequence

697. Degree of an Array

2444. Count Subarrays With Fixed Bounds

Explanation: https://leetcode.com/problems/count-subarrays-with-fixed-bounds/editorial

Last updated

Was this helpful?