Two Pointer cont

题型

1. 排序:两个或多个array(一般是sorted)按照某规则排序

88. Merge Sorted Array
283. Move Zeroes
56. Merge Intervals
57. Insert Interval

2. 窗口内最大,最小或sum

209. Minimum Size Subarray Sum
42. Trapping Rain Water
11. Container With Most Water

3. 在字符串或数组中找substring或者subarray

344.Reverse String
186.Reverse Words in a String II
3. Longest Substring Without Repeating Characters
28. Implement strStr()
345. Reverse Vowels of a String
76. Minimum Window Substring
30. Substring with Concatenation of All Words
159. Longest Substring with At Most Two Distinct Characters
487. Max Consecutive Ones II
567. Permutation in String

4.在字符串或数组中找组合

167. Two Sum II - Input array is sorted
15. 3Sum
259. 3Sum Smaller
16. 3Sum Closest
18. 4Sum

5.建立map(array map or bitmap),查询要求结果

383. Ransom Note
532. K-diff Pairs in an Array

6. 快慢指针

• Find the Middle of Linked List
141. Linked List Cycle I
142. Linked List Cycle II
234. Palindrome Linked List
19.Remove Nth Node From End of List
86. Partition List

7.线性扫描找目标

27. Remove Element
26. Remove Duplicates from Sorted Array
80. Remove Duplicates from Sorted Array II
349. Intersection of Two Arrays
350. Intersection of Two Arrays II
126. Valid Palindrome
75. Sort Colors
61. Rotate List
143. Reorder List
632. Smallest Range
360. Sort Transformed Array
524. Longest Word in Dictionary through Deleting

套路

Scanner

注意

旧的练习

88.merge two sorted array

  public void merge(int[] nums1, int m, int[] nums2, int n) {
        while(n > 0){
            nums1[n + m - 1] = (m == 0 || nums2[n - 1] > nums1[m - 1]) ? nums2[--n] : nums1[--m];
        }
    }

167.Two Sum II - Input array is sorted

  public int[] twoSum(int[] nums, int target) {
        // write your code here
        int[] res = new int[2];
        HashMap map = new HashMap<>();
        int left = 0;
        int right = nums.length - 1;
        
        while (left < right){
            int cur = nums[left] + nums[right];
            if (cur == target){
                return new int[]{left + 1, right + 1};
            } else if (cur < target){
                left++;
            } else {
                right--;
            }
        }
        return res;
    }

新的练习

你可能感兴趣的:(Two Pointer cont)