# Problem 163: Missing Ranges

> <https://leetcode.com/problems/missing-ranges/>

![](/files/-Lpv9yfO_PhiOw8sbYPJ)

## 思路

* 这道题的麻烦之处就在于，是否有 lower -> 第一个元素， 最后一个元素 -> upper，以及什么时候是单个元素，什么时候要加箭头
* 我们可以用双指针来实现，prev 和 cur。当他们之间相差大于 1 的时候就证明有一段丢失了。

```java
public class Solution {
    public List<String> findMissingRanges(int[] nums, int lower, int upper) {
        List<String> rst = new ArrayList<String>();
        long prev = lower - 1;
        long cur = 0;
        for (int i = 0; i <= nums.length; i++) {
            cur = i == nums.length ? upper + 1 : nums[i];
            if (cur - prev > 1) {
                rst.add(getRange(prev + 1, cur - 1));
            }
            prev = cur;
        }

        return rst;
    }

    private String getRange(long start, long end) {
        return start == end ? String.valueOf(start) : start + "->" + end;
    }
}
```

## 易错点

1. 元素越界，所以用 long

   ```java
   [2147483647]
   0, 2147483647
   ```

   这个 test case 就会越界
2. 循环次数

   ```java
   for (int i = 0; i <= nums.length; i++)
   ```

   之所以是 `<=`是因为要考虑最后一个元素到 upper 这一段


---

# 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://liuyang89116.gitbook.io/my-leetcode-book/post_chapter_3_array/problem_163_missing_ranges.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.
