双指针技巧框架

双指针分类

双指针一般分为两类:快慢指针和左右指针。

快慢指针主要解决链表问题,比如判断链表中是否有环、环的结点在哪

左右指针主要解决数组(或字符串)问题,比如二分查找,滑动窗口等。

快慢指针常见用法

1.判断链表中是否有环

boolean hasCycle(ListNode head) {
    ListNode fast, slow;
    fast = slow = head;
    while (fast != null && fast.next != null) {
        fast = fast.next.next;
        slow = slow.next;

        if (fast == slow) return true;
    }
    return false;
}

2.找出链表中环的起始位置

ListNode detectCycle(ListNode head) {
    ListNode fast, slow;
    fast = slow = head;
    while (fast != null && fast.next != null) {
        fast = fast.next.next;
        slow = slow.next;
        
        if (fast == slow){
			slow = head;
		    while (slow != fast) {
		        fast = fast.next;
		        slow = slow.next;
		    }
		    return slow;
		}
    }
    return null;
}

3.找到链表的中点

ListNode middleNode(ListNode head) {
    ListNode fast, slow;
    fast = slow = head;
    while (fast != null && fast.next != null) {
        fast = fast.next.next;
        slow = slow.next;
    }
    // slow 就在中间位置
    return slow;
}

左右指针常见用法

1.二分查找

二分查找算法模板

2.两数之和问题

int[] twoSum(int[] nums, int target) {
    int left = 0, right = nums.length - 1;
    while (left < right) {
        int sum = nums[left] + nums[right];
        if (sum == target) {
            // 题目要求的索引是从 1 开始的
            return new int[]{left + 1, right + 1};
        } else if (sum < target) {
            left++; // 让 sum 大一点
        } else if (sum > target) {
            right--; // 让 sum 小一点
        }
    }
    return new int[]{-1, -1};
}

3.滑动窗口

滑动窗口算法模板


版权声明:本文为weixin_42870497原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。