|
| 1 | +<h2><a href="https://leetcode.com/problems/summary-ranges/">228. Summary Ranges</a></h2><h3>Easy</h3><hr><div><p>You are given a <strong>sorted unique</strong> integer array <code>nums</code>.</p> |
| 2 | + |
| 3 | +<p>A <strong>range</strong> <code>[a,b]</code> is the set of all integers from <code>a</code> to <code>b</code> (inclusive).</p> |
| 4 | + |
| 5 | +<p>Return <em>the <strong>smallest sorted</strong> list of ranges that <strong>cover all the numbers in the array exactly</strong></em>. 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>.</p> |
| 6 | + |
| 7 | +<p>Each range <code>[a,b]</code> in the list should be output as:</p> |
| 8 | + |
| 9 | +<ul> |
| 10 | + <li><code>"a->b"</code> if <code>a != b</code></li> |
| 11 | + <li><code>"a"</code> if <code>a == b</code></li> |
| 12 | +</ul> |
| 13 | + |
| 14 | +<p> </p> |
| 15 | +<p><strong class="example">Example 1:</strong></p> |
| 16 | + |
| 17 | +<pre><strong>Input:</strong> nums = [0,1,2,4,5,7] |
| 18 | +<strong>Output:</strong> ["0->2","4->5","7"] |
| 19 | +<strong>Explanation:</strong> The ranges are: |
| 20 | +[0,2] --> "0->2" |
| 21 | +[4,5] --> "4->5" |
| 22 | +[7,7] --> "7" |
| 23 | +</pre> |
| 24 | + |
| 25 | +<p><strong class="example">Example 2:</strong></p> |
| 26 | + |
| 27 | +<pre><strong>Input:</strong> nums = [0,2,3,4,6,8,9] |
| 28 | +<strong>Output:</strong> ["0","2->4","6","8->9"] |
| 29 | +<strong>Explanation:</strong> The ranges are: |
| 30 | +[0,0] --> "0" |
| 31 | +[2,4] --> "2->4" |
| 32 | +[6,6] --> "6" |
| 33 | +[8,9] --> "8->9" |
| 34 | +</pre> |
| 35 | + |
| 36 | +<p> </p> |
| 37 | +<p><strong>Constraints:</strong></p> |
| 38 | + |
| 39 | +<ul> |
| 40 | + <li><code>0 <= nums.length <= 20</code></li> |
| 41 | + <li><code>-2<sup>31</sup> <= nums[i] <= 2<sup>31</sup> - 1</code></li> |
| 42 | + <li>All the values of <code>nums</code> are <strong>unique</strong>.</li> |
| 43 | + <li><code>nums</code> is sorted in ascending order.</li> |
| 44 | +</ul> |
| 45 | +</div> |
0 commit comments