-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution228.go
More file actions
52 lines (42 loc) · 1019 Bytes
/
solution228.go
File metadata and controls
52 lines (42 loc) · 1019 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
41
42
43
44
45
46
47
48
49
50
51
52
package solution228
import (
"strconv"
)
// ============================================================================
// 228. Summary Ranges
// URL: https://leetcode.com/problems/summary-ranges/
// ============================================================================
/*
goos: linux
goarch: amd64
pkg: GoLeetCode/___unsolved/228---Summary-Ranges
cpu: 13th Gen Intel(R) Core(TM) i7-13700K
Benchmark_summaryRanges
Benchmark_summaryRanges-24 6932410 154.7 ns/op 120 B/op 5 allocs/op
PASS
*/
func summaryRanges(nums []int) []string {
var ans []string
if len(nums) == 0 {
return ans
}
var a, b int
for b < len(nums) {
if a != b {
if b == len(nums)-1 || nums[b] != nums[b+1]-1 {
n1 := strconv.Itoa(nums[a])
n2 := strconv.Itoa(nums[b])
ans = append(ans, n1+"->"+n2)
a = b + 1
}
} else {
if b == len(nums)-1 || nums[b] < nums[b+1]-1 {
n1 := strconv.Itoa(nums[b])
ans = append(ans, n1)
a = b + 1
}
}
b++
}
return ans
}