Skip to content

Commit 64bc427

Browse files
aQuaaQua
aQua
authored and
aQua
committed
228 finish
1 parent 4fe9190 commit 64bc427

File tree

2 files changed

+19
-11
lines changed

2 files changed

+19
-11
lines changed

Algorithms/0228.summary-ranges/summary-ranges.go

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,22 @@ func summaryRanges(a []int) []string {
1414

1515
begin := a[0]
1616
str := ""
17-
for i := 1; i < l; i++ {
18-
if a[i-1]+1 != a[i] {
19-
if a[i-1] == begin {
17+
18+
for i := 0; i < l; i++ {
19+
if i == l-1 || a[i]+1 != a[i+1] {
20+
if a[i] == begin {
2021
str = fmt.Sprintf("%d", begin)
2122
} else {
22-
str = fmt.Sprintf("%d->%d", begin, a[i-1])
23+
str = fmt.Sprintf("%d->%d", begin, a[i])
24+
}
25+
26+
if i+1 < l {
27+
begin = a[i+1]
2328
}
24-
begin = a[i]
29+
2530
res = append(res, str)
2631
}
2732
}
28-
if a[l-1] == begin {
29-
str = fmt.Sprintf("%d", begin)
30-
} else {
31-
str = fmt.Sprintf("%d->%d", begin, a[l-1])
32-
}
33-
res = append(res, str)
3433

3534
return res
3635
}

Algorithms/0228.summary-ranges/summary-ranges_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,15 @@ func Test_Problem0228(t *testing.T) {
2727

2828
qs := []question{
2929

30+
question{
31+
para{
32+
[]int{},
33+
},
34+
ans{
35+
[]string{},
36+
},
37+
},
38+
3039
question{
3140
para{
3241
[]int{0, 1, 2, 4, 5, 7},

0 commit comments

Comments
 (0)