# LC 228. Summary Ranges ### [Problem link](https://leetcode.com/problems/summary-ranges/) ###### tags: `leedcode` `python` `easy` `Two Pointer` You are given a **sorted unique** integer array <code>nums</code>. A **range** <code>[a,b]</code> is the set of all integers from <code>a</code> to <code>b</code> (inclusive). Return the **smallest sorted** list of ranges that **cover all the numbers in the array exactly** . That is, each element of <code>nums</code> is covered by exactly one of the ranges, and there is no integer <code>x</code> such that <code>x</code> is in one of the ranges but not in <code>nums</code>. Each range <code>[a,b]</code> in the list should be output as: - <code>"a->b"</code> if <code>a != b</code> - <code>"a"</code> if <code>a == b</code> **Example 1:** ``` Input: nums = [0,1,2,4,5,7] Output: ["0->2","4->5","7"] Explanation: The ranges are: [0,2] --> "0->2" [4,5] --> "4->5" [7,7] --> "7" ``` **Example 2:** ``` Input: nums = [0,2,3,4,6,8,9] Output: ["0","2->4","6","8->9"] Explanation: The ranges are: [0,0] --> "0" [2,4] --> "2->4" [6,6] --> "6" [8,9] --> "8->9" ``` **Constraints:** - <code>0 <= nums.length <= 20</code> - <code>-2<sup>31</sup> <= nums[i] <= 2<sup>31</sup> - 1</code> - All the values of <code>nums</code> are **unique** . - <code>nums</code> is sorted in ascending order. ## Solution 1 - Two pointer ```python= class Solution: def summaryRanges(self, nums: List[int]) -> List[str]: res = [] n = len(nums) left, right = 0, 0 while right < n: if right + 1 >= n or nums[right] + 1 != nums[right + 1]: if left != right: res.append(f"{nums[left]}->{nums[right]}") else: res.append(str(nums[left])) left = right + 1 right += 1 return res ``` >### Complexity >| | Time Complexity | Space Complexity | >| ----------- | --------------- | ---------------- | >| Solution 1 | O(n) | O(n) | ## Note x