🧩 Problem Statement
Given a sorted integer array without duplicates, return the summary of its ranges.
A range [a,b]
is represented as "a->b"
if a != b
. Otherwise, it is represented as "a"
.
You need to implement the following function:
go
func summaryRanges(nums []int) []string
📘 Example
go
Input: nums = [0,1,2,4,5,7]
Output: ["0->2","4->5","7"]
Input: nums = [0,2,3,4,6,8,9]
Output: ["0","2->4","6","8->9"]
📝 Explanation:
- Example 1:
- The input is
[0,1,2,4,5,7]
.
- We can summarize the ranges as
"0->2"
, "4->5"
, and "7"
.
- Example 2:
- The input is
[0,2,3,4,6,8,9]
.
- The ranges are
"0"
, "2->4"
, "6"
, and "8->9"
.
🧠 Approach
To solve the problem, we can follow this strategy:
- Iterate through the sorted list and find continuous sequences (ranges).
- Identify the start and end of each range:
- A range starts at the first number of a sequence.
- A range ends when the current number is not consecutive with the previous number.
- Handle edge cases:
- If there is only one number in the sequence, return it as a single number (e.g.,
"5"
instead of "5->5"
).
- Store and format the ranges appropriately.
✅ Go Implementation
go
package main
import (
"fmt"
"strconv"
)
// Function to find and return the summary ranges
func summaryRanges(nums []int) []string {
if len(nums) == 0 {
return []string{}
}
var result []string
start := nums[0]
for i := 1; i <= len(nums); i++ {
// If we reach the end or find a non-consecutive number
if i == len(nums) || nums[i] != nums[i-1]+1 {
if start == nums[i-1] {
result = append(result, strconv.Itoa(start))
} else {
result = append(result, strconv.Itoa(start)+"->"+strconv.Itoa(nums[i-1]))
}
if i < len(nums) {
start = nums[i]
}
}
}
return result
}
// Helper function to test the summaryRanges function
func main() {
// Test cases
fmt.Println(summaryRanges([]int{0, 1, 2, 4, 5, 7})) // Output: ["0->2", "4->5", "7"]
fmt.Println(summaryRanges([]int{0, 2, 3, 4, 6, 8, 9})) // Output: ["0", "2->4", "6", "8->9"]
fmt.Println(summaryRanges([]int{1, 3, 4, 5, 7, 8, 9})) // Output: ["1", "3->5", "7->9"]
fmt.Println(summaryRanges([]int{1})) // Output: ["1"]
fmt.Println(summaryRanges([]int{0, 1, 2, 3, 4})) // Output: ["0->4"]
}
📦 Example Usage
go
func main() {
// Test cases
fmt.Println(summaryRanges([]int{0, 1, 2, 4, 5, 7})) // Output: ["0->2", "4->5", "7"]
fmt.Println(summaryRanges([]int{0, 2, 3, 4, 6, 8, 9})) // Output: ["0", "2->4", "6", "8->9"]
fmt.Println(summaryRanges([]int{1, 3, 4, 5, 7, 8, 9})) // Output: ["1", "3->5", "7->9"]
fmt.Println(summaryRanges([]int{1})) // Output: ["1"]
fmt.Println(summaryRanges([]int{0, 1, 2, 3, 4})) // Output: ["0->4"]
}
⏱️ Time & Space Complexity
- Time Complexity:
- O(n), where
n
is the number of elements in the nums
array. We are iterating through the list once.
- Space Complexity:
- O(k), where
k
is the number of ranges we return. In the worst case, k
could be equal to n
, where every number is a separate range.
This solution efficiently handles summarizing ranges in a sorted integer list. The approach ensures that we consider consecutive sequences and handle edge cases where there might be a single number.