Skip to content

Commit

Permalink
Time: 1 ms (82.40%), Space: 2.1 MB (65.95%) - LeetHub
Browse files Browse the repository at this point in the history
  • Loading branch information
hugehoo committed May 9, 2024
1 parent b01134f commit e9b3d51
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions 0228-summary-ranges/0228-summary-ranges.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
func summaryRanges(nums []int) []string {
counter := 0
var list []string

for i, num := range nums {
nextNum := num + 2
if i != len(nums)-1 {
nextNum = nums[i+1]
}

if nextNum-num <= 1 {
counter++
continue
}

startNum := num - counter
if counter == 0 {
list = append(list, fmt.Sprintf("%d", num))
} else {
list = append(list, fmt.Sprintf("%d->%d", startNum, num))
counter = 0
}

}
return list
}

0 comments on commit e9b3d51

Please sign in to comment.