add new solutions

This commit is contained in:
kanna5 2025-12-23 17:59:03 +09:00
parent ccb8b5673b
commit 58527849b2
Signed by: kkyy
GPG key ID: 06332F3965E9B0CF
6 changed files with 202 additions and 0 deletions

View file

@ -0,0 +1,32 @@
package q228
import (
"fmt"
"strconv"
)
func addToRanges(output []string, a, b int) []string {
if a == b {
return append(output, strconv.FormatInt(int64(a), 10))
}
return append(output, fmt.Sprintf("%d->%d", a, b))
}
func summaryRanges(nums []int) []string {
if len(nums) == 0 {
return nil
}
output := []string{}
l := 0
for r := 1; r < len(nums); r++ {
if nums[r]-nums[r-1] != 1 {
output = addToRanges(output, nums[l], nums[r-1])
l = r
}
}
output = addToRanges(output, nums[l], nums[len(nums)-1])
return output
}
var _ = summaryRanges