mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-01-11 02:58:13 +08:00
47 lines
1.7 KiB
HTML
47 lines
1.7 KiB
HTML
<p>You are given a <strong>sorted unique</strong> integer array <code>nums</code>.</p>
|
|
|
|
<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>
|
|
|
|
<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>
|
|
|
|
<p>Each range <code>[a,b]</code> in the list should be output as:</p>
|
|
|
|
<ul>
|
|
<li><code>"a->b"</code> if <code>a != b</code></li>
|
|
<li><code>"a"</code> if <code>a == b</code></li>
|
|
</ul>
|
|
|
|
<p> </p>
|
|
<p><strong>Example 1:</strong></p>
|
|
|
|
<pre>
|
|
<strong>Input:</strong> nums = [0,1,2,4,5,7]
|
|
<strong>Output:</strong> ["0->2","4->5","7"]
|
|
<strong>Explanation:</strong> The ranges are:
|
|
[0,2] --> "0->2"
|
|
[4,5] --> "4->5"
|
|
[7,7] --> "7"
|
|
</pre>
|
|
|
|
<p><strong>Example 2:</strong></p>
|
|
|
|
<pre>
|
|
<strong>Input:</strong> nums = [0,2,3,4,6,8,9]
|
|
<strong>Output:</strong> ["0","2->4","6","8->9"]
|
|
<strong>Explanation:</strong> The ranges are:
|
|
[0,0] --> "0"
|
|
[2,4] --> "2->4"
|
|
[6,6] --> "6"
|
|
[8,9] --> "8->9"
|
|
</pre>
|
|
|
|
<p> </p>
|
|
<p><strong>Constraints:</strong></p>
|
|
|
|
<ul>
|
|
<li><code>0 <= nums.length <= 20</code></li>
|
|
<li><code>-2<sup>31</sup> <= nums[i] <= 2<sup>31</sup> - 1</code></li>
|
|
<li>All the values of <code>nums</code> are <strong>unique</strong>.</li>
|
|
<li><code>nums</code> is sorted in ascending order.</li>
|
|
</ul>
|