Skip to content

Commit 1bc4689

Browse files
committed
Add solution and test-cases for problem 228
1 parent 6e07ff8 commit 1bc4689

File tree

3 files changed

+58
-21
lines changed

3 files changed

+58
-21
lines changed

leetcode/201-300/0228.Summary-Ranges/README.md

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,37 @@
11
# [228.Summary Ranges][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
You are given a **sorted unique** integer array nums.
5+
6+
Return the **smallest sorted** list of ranges that **cover all the numbers in the array exactly**. That is, each element of `nums` is covered by exactly one of the ranges, and there is no integer `x` such that `x` is in one of the ranges but not in `nums`.
7+
8+
Each range `[a,b]` in the list should be output as:
9+
10+
- `"a->b"` if `a != b`
11+
- `"a"` if `a == b`
712

813
**Example 1:**
914

1015
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
16+
Input: nums = [0,1,2,4,5,7]
17+
Output: ["0->2","4->5","7"]
18+
Explanation: The ranges are:
19+
[0,2] --> "0->2"
20+
[4,5] --> "4->5"
21+
[7,7] --> "7"
1322
```
1423

15-
## 题意
16-
> ...
24+
**Example 2:**
1725

18-
## 题解
19-
20-
### 思路1
21-
> ...
22-
Summary Ranges
23-
```go
2426
```
25-
27+
Input: nums = [0,2,3,4,6,8,9]
28+
Output: ["0","2->4","6","8->9"]
29+
Explanation: The ranges are:
30+
[0,0] --> "0"
31+
[2,4] --> "2->4"
32+
[6,6] --> "6"
33+
[8,9] --> "8->9"
34+
```
2635

2736
## 结语
2837

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,31 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
import (
4+
"fmt"
5+
)
6+
7+
func Solution(nums []int) []string {
8+
result := make([]string, 0)
9+
if len(nums) == 0 {
10+
return result
11+
}
12+
start, end := 0, 1
13+
for ; end < len(nums); end++ {
14+
if nums[end]-nums[end-1] == 1 {
15+
continue
16+
}
17+
18+
writeObj := fmt.Sprintf("%d", nums[start])
19+
if end-start != 1 {
20+
writeObj = fmt.Sprintf("%d->%d", nums[start], nums[end-1])
21+
}
22+
result = append(result, writeObj)
23+
start = end
24+
}
25+
writeObj := fmt.Sprintf("%d", nums[start])
26+
if end-start != 1 {
27+
writeObj = fmt.Sprintf("%d->%d", nums[start], nums[end-1])
28+
}
29+
result = append(result, writeObj)
30+
return result
531
}

leetcode/201-300/0228.Summary-Ranges/Solution_test.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs []int
14+
expect []string
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", []int{0, 1, 2, 4, 5, 7}, []string{"0->2", "4->5", "7"}},
17+
{"TestCase2", []int{0, 2, 3, 4, 6, 8, 9}, []string{"0", "2->4", "6", "8->9"}},
18+
{"TestCase3", []int{1, 2, 3}, []string{"1->3"}},
19+
{"TestCase4", []int{0, 1, 2, 4, 5, 6, 8}, []string{"0->2", "4->6", "8"}},
20+
{"TestCase5", []int{1, 2, 3, 5}, []string{"1->3", "5"}},
1921
}
2022

2123
// 开始测试

0 commit comments

Comments
 (0)