-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsummaryRanges.go
More file actions
40 lines (32 loc) · 769 Bytes
/
Copy pathsummaryRanges.go
File metadata and controls
40 lines (32 loc) · 769 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/* https://leetcode.com/problems/summary-ranges/description/
Given a sorted integer array without duplicates, return the summary of its ranges.
Example 1:
Input: [0,1,2,4,5,7]
Output: ["0->2","4->5","7"]
Example 2:
Input: [0,2,3,4,6,8,9]
Output: ["0","2->4","6","8->9"]
*/
package larray
import "strconv"
func summaryRanges(nums []int) []string {
length := len(nums)
if length == 0 {
return []string{}
}
res := []string{}
for left, i := nums[0], 1; i <= len(nums); i++ {
if i == length || nums[i] != nums[i-1]+1 {
right := nums[i-1]
if left == right {
res = append(res, strconv.Itoa(left))
} else {
res = append(res, strconv.Itoa(left)+"->"+strconv.Itoa(right))
}
if i < length {
left = nums[i]
}
}
}
return res
}