---
# System prepended metadata

title: \#228 Summary Ranges
tags: [leetcode, leetcode-easy]

---

# \#228 Summary Ranges
## *給定一不重複且已升冪排序整數數字陣列, 輸出一字串陣列, 表示輸入陣列的各段連續數字區間*
### Ex.input:[0,1,2,3,5,6,8], output:['0->3','5->6','8']

## Log
 - build 20210506 by syhuang

## 初見
 - 差點沒明白題意, 了解後剩下邏輯實作:用兩變數暫存區間, 迴圈判斷input每個元素是否為連續, 連續時更新numEnd; 不連續時就紀錄區間(含判斷區是否只有一個數字)
```javascript=
var summaryRanges = function(nums) {
    if(nums.length == 0) return [];
    var result = [];
    var numFrom = nums[0];
    var numEnd = nums[0];
    for(var i=1; i<nums.length; i++){
        if(nums[i] - numEnd != 1){
            if(numFrom != numEnd) result.push(numFrom.toString()+'->'+numEnd);
            else result.push(numFrom+'');
            numFrom = numEnd = nums[i];
        }else{
            numEnd = nums[i];
        }
    }
    if(numFrom != numEnd) result.push(numFrom+'->'+numEnd);
    else result.push(numFrom+'');
    
    return result;
};
```
## 備註
 - 初見即畢業
 - 爛數有點多, 討論度不高
## 參考
###### tags: `leetcode`, `leetcode-easy`