add new solutions
This commit is contained in:
parent
ccb8b5673b
commit
58527849b2
6 changed files with 202 additions and 0 deletions
32
solutions/2/q228/solution.go
Normal file
32
solutions/2/q228/solution.go
Normal 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
|
||||
Loading…
Add table
Add a link
Reference in a new issue