mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-04 16:12:47 +08:00
20 lines
321 B
Go
20 lines
321 B
Go
package leetcode
|
|
|
|
import (
|
|
"strconv"
|
|
)
|
|
|
|
func summaryRanges(nums []int) (ans []string) {
|
|
for i, n := 0, len(nums); i < n; {
|
|
left := i
|
|
for i++; i < n && nums[i-1]+1 == nums[i]; i++ {
|
|
}
|
|
s := strconv.Itoa(nums[left])
|
|
if left != i-1 {
|
|
s += "->" + strconv.Itoa(nums[i-1])
|
|
}
|
|
ans = append(ans, s)
|
|
}
|
|
return
|
|
}
|