> For the complete documentation index, see [llms.txt](https://r24zeng.gitbook.io/leetcode-notebook/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://r24zeng.gitbook.io/leetcode-notebook/bu-chong-81100-dao/228.-summary-ranges.md).

# 228. Summary Ranges

\# Medium

{% hint style="info" %}
Key idea: three veriables, start point of range, end point of range, current point.

Compare current number to the last one.
{% endhint %}

{% tabs %}
{% tab title="Python" %}

```python
class Solution:
    def summaryRanges(self, nums: List[int]) -> List[str]:
        res = []
        # edge case
        if len(nums) == 0:
            return []
        elif len(nums) == 1:
            res.append(str(nums[0]))
            return res
        
        left = 0
        right = 0
        i = 1
        while i < len(nums):
            if nums[i] == nums[right] + 1:
                right += 1
                i += 1
                continue
            # if find the range
            if left == right:
                res.append(str(nums[left]))
            else:
                tmp = str(nums[left]) + "->" + str(nums[right])
                res.append(tmp)
            left = i
            right = i
            i += 1
            
        if left == right:
            res.append(str(nums[left]))
        else:
            tmp = str(nums[left]) + "->" + str(nums[right])
            res.append(tmp)
        return res
```

{% endtab %}
{% endtabs %}
