Class Solution

java.lang.Object
g0201_0300.s0228_summary_ranges.Solution

public class Solution extends Object
228 - Summary Ranges.<p>Easy</p> <p>You are given a <strong>sorted unique</strong> integer array <code>nums</code>.</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>&quot;a->b&quot;</code> if <code>a != b</code></li> <li><code>&quot;a&quot;</code> if <code>a == b</code></li> </ul> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> nums = [0,1,2,4,5,7]</p> <p><strong>Output:</strong> [&ldquo;0->2&rdquo;,&ldquo;4->5&rdquo;,&ldquo;7&rdquo;]</p> <p><strong>Explanation:</strong> The ranges are: [0,2] &ndash;> &ldquo;0->2&rdquo; [4,5] &ndash;> &ldquo;4->5&rdquo; [7,7] &ndash;> &ldquo;7&rdquo;</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> nums = [0,2,3,4,6,8,9]</p> <p><strong>Output:</strong> [&ldquo;0&rdquo;,&ldquo;2->4&rdquo;,&ldquo;6&rdquo;,&ldquo;8->9&rdquo;]</p> <p><strong>Explanation:</strong> The ranges are: [0,0] &ndash;> &ldquo;0&rdquo; [2,4] &ndash;> &ldquo;2->4&rdquo; [6,6] &ndash;> &ldquo;6&rdquo; [8,9] &ndash;> &ldquo;8->9&rdquo;</p> <p><strong>Example 3:</strong></p> <p><strong>Input:</strong> nums = []</p> <p><strong>Output:</strong> []</p> <p><strong>Example 4:</strong></p> <p><strong>Input:</strong> nums = [-1]</p> <p><strong>Output:</strong> [&ldquo;-1&rdquo;]</p> <p><strong>Example 5:</strong></p> <p><strong>Input:</strong> nums = [0]</p> <p><strong>Output:</strong> [&ldquo;0&rdquo;]</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>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • summaryRanges

      public List<String> summaryRanges(int[] nums)