# 228. Summary Ranges (1)

[Link](https://leetcode.com/problems/summary-ranges/)

Given a sorted integer array without duplicates, return the summary of its ranges.

**Example 1:**

```
Input:  [0,1,2,4,5,7]
Output: ["0->2","4->5","7"]
Explanation: 0,1,2 form a continuous range; 4,5 form a continuous range.
```

**Example 2:**

```
Input:  [0,2,3,4,6,8,9]
Output: ["0","2->4","6","8->9"]
Explanation: 2,3,4 form a continuous range; 8,9 form a continuous range.
```

**Solution**

因為是sorted array所以後續的element一定大於等於之前的element，當發現後面的element大於目前的interval high bound:\
只大於 1，更新目前interval的 high bound\
大於2以上，把目前interval更新到list裡，並重新建立下個interval\\

```
class Solution {
    public List<String> summaryRanges(int[] nums) {
        int len = nums.length;
        List<String> result = new LinkedList<>();
        if(len == 0)
        {
            return result;
        }
        if(len == 1){
            result.add(""+nums[0]);
            return result;
        }
        
        int start = nums[0];
        int end = nums[0];
        for(int i = 1; i < len; i++){
            if(end+1 == nums[i]){
                end = nums[i];
            }
            else{
                if(start != end){
                    result.add(start+"->"+end);
                }
                else{
                    result.add(""+start);
                }
                start = nums[i];
                end = nums[i];
            }
        }
        if(start != end){
            result.add(start+"->"+end);
        }else{
            result.add(""+start);
        }
        return result;
    }
}
```
