---
# System prepended metadata

title: 0228. Summary Ranges
tags: [Leetcode, FaceBook, Easy]

---

# 0228. Summary Ranges
###### tags: `Leetcode` `Easy` `FaceBook`
Link: https://leetcode.com/problems/summary-ranges/
## 思路
和[0163. Missing Ranges](https://hackmd.io/S_blCxh8TiOyYKqCXWP2xw)差不多
## Code
```java=
class Solution {
    public List<String> summaryRanges(int[] nums) {
        List<String> ans = new ArrayList<>();
        if(nums.length==0) return ans;
        int start = nums[0];
        int end = nums[0];
        for(int i = 1;i < nums.length;i++){
            if(nums[i]==end+1){
                end = nums[i];
            }
            else{
                ans.add(ranges(start, end));
                start = nums[i];
                end = nums[i];
            }
        }
        ans.add(ranges(start, end));
        return ans;
    }
    private String ranges(int a, int b){
        if(a!=b){
            return a+"->"+b;
        }
        else{
            return Integer.toString(a);
        }
    }
}
```