双指针顾名思义就是通过两个指针来对数组、链表等一些线行数据结果进行遍历来规整出符合题目描述的问题结果,例如通过快慢指针找到链表的中间节点、通过左右夹逼指针找出某个区间的最大值等等。对于哪种类型的题而言是能够采用上指针法来解决的,一般需要满足几个条件:
    1、题目本身的数据结构为:数组或者链表这类线性结构
    2、题目的问题是:针对数组的某个区间

    下面以几个典型的双指针法解题:

    • 三数之和

    image.png
    解题:首先要去分析该题如何解决?以往我们一般看到的都是两数之和,直接创建一个哈希表进行查找即可,但是到了三数之和后,发现通过哈希表解决起来难度变大,实现起来考虑情况也变得复杂。那么我们能不能采用固定一个数,然后再然另外两个数再某一个区间里面变动并求和比较大小,之后再根据大小比较的情况继续移动另外两个数。这个思路听起来是比较合适的,也是大家能普遍想到的解决方案,但是其中有个问题,就是两个数的指针应该如何移动并且不会重复?这个解决起来也比较简单,直接对数组进行排序,那么根据三数之和与0之间大小比较,即可移动右区间的另外两个指针,这下思路就比较清晰了,具体代码如下:

    1. class Solution {
    2. public List<List<Integer>> threeSum(int[] nums) {
    3. int len = nums.length;
    4. Arrays.sort(nums);
    5. List<List<Integer>> list = new ArrayList<>();
    6. for (int i = 0; i < len - 2; i++) {
    7. if (i > 0 && nums[i] == nums[i - 1]) {
    8. continue;
    9. }
    10. // 设置双指针
    11. int j = i + 1, k = len - 1;
    12. // 作为首尾双指针(主要目的是夹逼,越来越逼近目标值)
    13. while (j < k) {
    14. int sum = nums[i] + nums[j] + nums[k];
    15. if (sum < 0) {
    16. j++;
    17. while (j < k && nums[j] == nums[j - 1]) j++;
    18. } else if (sum > 0) {
    19. k--;
    20. while (j < k && nums[k] == nums[k + 1]) k--;
    21. } else {
    22. list.add(Arrays.asList(nums[i], nums[j], nums[k]));
    23. j++;
    24. k--;
    25. while (j < k && nums[j] == nums[j - 1]) j++;
    26. while (j < k && nums[k] == nums[k + 1]) k--;
    27. }
    28. }
    29. }
    30. return list;
    31. }
    32. }
    • 原地删除排序数组中的重复项

    image.png
    解题:最经典的双指针类型题,快指针向右移动,并且与满指针对应下标元素进行比较,当不相等时快慢指针同时右移,相等时仅仅快指针右移动。

    1. class Solution {
    2. public void removeDuplicates(int[] nums) {
    3. int left = 0, right = 0;
    4. while (right < nums.length) {
    5. if (nums[right] != nums[left]) {
    6. nums[++left] = nums[right];
    7. }
    8. right++;
    9. }
    10. return ArrayscopyOfRange(nums, 0, left + 1);
    11. }
    12. }
    • 接雨水问题:

    image.png
    解题:接雨水问题的关键在于如何找到凹槽部分,当利用双指针解法时左右指针分别从数组的其实下标与终止下标进行区间缩紧,在缩紧过程中需要找到左部分的最大值leftmax以及右部分的最大值rightmax,也就是该容器当前能承受的高度。之后计算遍历当前柱子与max高度之间的差值并求和,最终得到接雨水的容量。

    1. class Solution {
    2. public int trap(int[] nums) {
    3. int left = 0, right = nums.length - 1;
    4. int leftMax = 0, rightMax = 0;
    5. int result = 0;
    6. while (left < right) {
    7. if (nums[left] < nums[right]) {
    8. if (nums[left] > leftMax) {
    9. leftMax = nums[left];
    10. } else {
    11. result += leftMax - nums[left];
    12. }
    13. left++;
    14. } else {
    15. if (nums[right] > rightMax) {
    16. rightMax = nums[right];
    17. } else {
    18. result += rightMax - nums[right];
    19. }
    20. right++;
    21. }
    22. }
    23. return result;
    24. }
    25. }
    • 快慢指针的常见算法:

      • 判断链表是否有环:

        1. boolean hasCycle(ListNode head) {
        2. ListNode slow, fast;
        3. slow = fast = head;
        4. // fast不为空,fast.next不为空
        5. while (fast != null && fast.next != null) {
        6. slow = slow.next;
        7. fast = fast.next.next;
        8. if (fast == slow) {
        9. return true;
        10. }
        11. }
        12. return false;
        13. }
      • 寻找链表的中点:

        1. ListNode findMidNode(ListNode head) {
        2. ListNode slow, fast;
        3. slow = fast = head;
        4. // 如果链表节点数为奇数,那么slow节点在中间;如果链表节点数为偶数,那么slow节点在偏右侧
        5. while (fast != null && fast.next != null) {
        6. fast = fast.next.next;
        7. slow = slow.next;
        8. }
        9. return slow;
        10. }
      • 反转数组:

        1. void reverse(int[] nums) {
        2. int left = 0, right = nums.length - 1;
        3. while (left < right) {
        4. int temp = nums[left];
        5. nums[left++] = nums[right];
        6. nums[right--] = temp;
        7. }
        8. }
      • 滑动窗口算法模板:

        1. int left = 0, right = 0;
        2. while (right < s.size()) {
        3. // 增大窗口
        4. window.add(s[right]);
        5. right++;
        6. while (window needs shrink) {
        7. //缩小窗口
        8. window.remove(s[left]);
        9. left++;
        10. }
        11. }

        该算法的模板解析,以例题来解决:
        image.png
        解题:该题是一道典型的滑动窗口题,首先移动right右指针满足字符串包括T串中所有字符,之后移动左子串对区间进行压缩;

        1. class Solution {
        2. public String minWindow(String s, String t) {
        3. //哈希表相当于管理窗口的容器
        4. HashMap<Character, Integer> map = new HashMap<>();
        5. for (char c : t.toCharArray()) {
        6. map.put(c, map.getOrDefault(c, 0) + 1);
        7. }
        8. String result = "";
        9. // cnt表示t字符串包含的字符
        10. int cnt = map.size();
        11. // 双指针,right右移动
        12. int left = 0, right = 0;
        13. while (right < s.length()) {
        14. char chr = s.charAt(right);
        15. if (map.containsKey(chr)) {
        16. map.put(chr, map.get(chr) - 1);
        17. if (map.get(chr) == 0) {
        18. cnt--;
        19. }
        20. }
        21. // 设置窗口缩小条件
        22. while (cnt == 0) {
        23. if (result.isEmpty() || result.length() > right - left + 1) {
        24. result = s.substring(left, right + 1);
        25. }
        26. // left下标如果去除后会带来什么后果
        27. char crh = s.charAt(left);
        28. if (map.containsKey(crh)) {
        29. if (map.get(crh) == 0) {
        30. cnt++;
        31. }
        32. map.put(crh, map.get(crh) + 1);
        33. }
        34. // left指针右移
        35. left++;
        36. }
        37. // right指针右移
        38. right++;
        39. }
        40. return result;
        41. }
        42. }