Summary Ranges

Summary Ranges

You are given a sorted unique integer array nums.

A range [a,b] is the set of all integers from a to b (inclusive).

Return the smallest sorted list of ranges that cover all the numbers in the array exactly. That is, each element of nums is covered by exactly one of the ranges, and there is no integer x such that x is in one of the ranges but not in nums.

Each range [a,b] in the list should be output as:

  • "a->b" if a != b
  • "a" if a == b

?

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:

  • 0 <= nums.length <= 20
  • -231 <= nums[i] <= 231 - 1
  • All the values of nums are unique.
  • nums is sorted in ascending order.


SOLUTION:



要查看或添加评论,请登录

Bekir Sahin的更多文章

  • Mastering Prompt Engineering: A Comprehensive Framework

    Mastering Prompt Engineering: A Comprehensive Framework

    In the rapidly evolving fields of artificial intelligence and data science, clear and precise instructions are vital…

    1 条评论
  • Simplify Path

    Simplify Path

    Given an absolute path for a Unix-style file system, which begins with a slash '/', transform this path into its…

  • Combinations

    Combinations

    Given two integers n and k, return all possible combinations of k numbers chosen from the range [1, n]. You may return…

  • Add Binary

    Add Binary

    Given two binary strings a and b, return their sum as a binary string. Example 1: Input: a = 11, b = 1 Output: 100…

  • Longest Substring Without Repeating Characters

    Longest Substring Without Repeating Characters

    Given a string s, find the length of the longest substring without repeating characters. Example 1: Input: s =…

  • Integer to Roman

    Integer to Roman

    Seven different symbols represent Roman numerals with the following values: Symbol Value I 1 V 5 X 10 L 50 C 100 D 500…

  • Minimum Size Subarray Sum

    Minimum Size Subarray Sum

    Given an array of positive integers nums and a positive integer target, return the minimal length of a subarray whose…

  • Reverse Words in a String

    Reverse Words in a String

    Given an input string s, reverse the order of the words. A word is defined as a sequence of non-space characters.

  • Rotate Array

    Rotate Array

    Given an integer array nums, rotate the array to the right by k steps, where k is non-negative. Example 1: Input: nums…

  • Best Time to Buy and Sell Stock II

    Best Time to Buy and Sell Stock II

    You are given an integer array prices where prices[i] is the price of a given stock on the i?? day. On each day, you…

社区洞察

其他会员也浏览了