---
# System prepended metadata

title: 0163. Missing Ranges
tags: [Leetcode, FaceBook, Easy]

---

# 0163. Missing Ranges
###### tags: `Leetcode` `Easy` `FaceBook`
Link: https://leetcode.com/problems/missing-ranges/
## 思路
按顺序遍历即可
但写法上很有讲究 要思考怎么把edge case合并一起处理~
## Code
```java=
class Solution {
    public List<String> findMissingRanges(int[] nums, int lower, int upper) {
        List<String> ans = new ArrayList<>();
        int prev = lower-1;
        for(int i = 0;i <= nums.length;i++){
            int curr = (i<nums.length)?nums[i]:upper+1;
            if(prev+1<=curr-1){
                ans.add(formatRange(prev+1, curr-1));
            }
            prev = curr;
        }
        return ans;
    }
    public String formatRange(int lower, int upper){
        if(lower == upper){
            return Integer.toString(lower);
        }
        else{
            return lower+"->"+upper;
        }
    }
}
```