# 双指针总结

双指针可以应用在不同的场景, 虽然都是用两个指针, 但用法, 即指针的移动方法大相径庭, 在移动方向, 移动逻辑, 移动速度上都可能有区别. 下面是刷题过程中遇到的经典双指针案例.

## 滑动窗口

**滑动窗口可以等价为双指针的一种**, 用前后两个指针控制窗口的范围.

例如在[\[3\]\[中等\]\[双指针\]\[滑动窗口\] 无重复字符的最长子串](broken://pages/-MFkbHl7OEQRTyoyYwK0)中, 使用双指针, 保持窗口内没有重复的字符.

再例如[\[209\]\[中等\]\[双指针\] 长度最小的子数组](broken://pages/-MFkbHl9vevMrKc_61Ze)中, 双指针控制窗口内子数组的数字之和在一定的范围内.

## 快慢指针

一般在链表中使用. 由于链表无法直接获取长度, 也无法像数组一样直接获取指定位置的元素, 因此如果我们要获取**固定分位数位置**的数值, 常常使用一快一慢两个指针. 例如为了获取链表中间位置的指针, 分别定义为`slow_ptr`和`fast_ptr`, `slow_ptr`每次移动一个结点, `fast_ptr`每次移动两个结点, 这样当`fast_ptr`移动到链表的截尾时, `slow_ptr`正好访问到中间节点, 无论链表的长度奇偶.

例如在[\[109\]\[中等\]\[DFS\]\[双指针\] 有序链表转换二叉搜索树](broken://pages/-MFkbHkzKHYWealbexN4)中就有使用上述的快慢指针.

## 更多

[双指针技巧总结](https://labuladong.gitbook.io/algo/suan-fa-si-wei-xi-lie/shuang-zhi-zhen-ji-qiao)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://blessbingo.gitbook.io/garnet/suan-fa/shuang-zhi-zhen/shuang-zhi-zhen-zong-jie.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
