Skip to content

Commit fbbc4ff

Browse files
committed
Add solution 228、1694、1695
1 parent 1bc9d34 commit fbbc4ff

16 files changed

+1618
-828
lines changed

README.md

+720-710
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package leetcode
2+
3+
import (
4+
"strconv"
5+
)
6+
7+
func summaryRanges(nums []int) (ans []string) {
8+
for i, n := 0, len(nums); i < n; {
9+
left := i
10+
for i++; i < n && nums[i-1]+1 == nums[i]; i++ {
11+
}
12+
s := strconv.Itoa(nums[left])
13+
if left != i-1 {
14+
s += "->" + strconv.Itoa(nums[i-1])
15+
}
16+
ans = append(ans, s)
17+
}
18+
return
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
type question228 struct {
9+
para228
10+
ans228
11+
}
12+
13+
// para 是参数
14+
// one 代表第一个参数
15+
type para228 struct {
16+
nums []int
17+
}
18+
19+
// ans 是答案
20+
// one 代表第一个答案
21+
type ans228 struct {
22+
ans []string
23+
}
24+
25+
func Test_Problem228(t *testing.T) {
26+
27+
qs := []question228{
28+
29+
{
30+
para228{[]int{0, 1, 2, 4, 5, 7}},
31+
ans228{[]string{"0->2", "4->5", "7"}},
32+
},
33+
34+
{
35+
para228{[]int{0, 2, 3, 4, 6, 8, 9}},
36+
ans228{[]string{"0", "2->4", "6", "8->9"}},
37+
},
38+
39+
{
40+
para228{[]int{}},
41+
ans228{[]string{}},
42+
},
43+
44+
{
45+
para228{[]int{-1}},
46+
ans228{[]string{"-1"}},
47+
},
48+
49+
{
50+
para228{[]int{0}},
51+
ans228{[]string{"0"}},
52+
},
53+
}
54+
55+
fmt.Printf("------------------------Leetcode Problem 228------------------------\n")
56+
57+
for _, q := range qs {
58+
_, p := q.ans228, q.para228
59+
fmt.Printf("【input】:%v 【output】:%v\n", p, summaryRanges(p.nums))
60+
}
61+
fmt.Printf("\n\n\n")
62+
}
+107
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
# [228. Summary Ranges](https://leetcode.com/problems/summary-ranges/)
2+
3+
## 题目
4+
5+
You are given a **sorted unique** integer array `nums`.
6+
7+
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`.
8+
9+
Each range `[a,b]` in the list should be output as:
10+
11+
- `"a->b"` if `a != b`
12+
- `"a"` if `a == b`
13+
14+
**Example 1:**
15+
16+
```
17+
Input: nums = [0,1,2,4,5,7]
18+
Output: ["0->2","4->5","7"]
19+
Explanation: The ranges are:
20+
[0,2] --> "0->2"
21+
[4,5] --> "4->5"
22+
[7,7] --> "7"
23+
24+
```
25+
26+
**Example 2:**
27+
28+
```
29+
Input: nums = [0,2,3,4,6,8,9]
30+
Output: ["0","2->4","6","8->9"]
31+
Explanation: The ranges are:
32+
[0,0] --> "0"
33+
[2,4] --> "2->4"
34+
[6,6] --> "6"
35+
[8,9] --> "8->9"
36+
37+
```
38+
39+
**Example 3:**
40+
41+
```
42+
Input: nums = []
43+
Output: []
44+
45+
```
46+
47+
**Example 4:**
48+
49+
```
50+
Input: nums = [-1]
51+
Output: ["-1"]
52+
53+
```
54+
55+
**Example 5:**
56+
57+
```
58+
Input: nums = [0]
59+
Output: ["0"]
60+
61+
```
62+
63+
**Constraints:**
64+
65+
- `0 <= nums.length <= 20`
66+
- `231 <= nums[i] <= 231 - 1`
67+
- All the values of `nums` are **unique**.
68+
- `nums` is sorted in ascending order.
69+
70+
## 题目大意
71+
72+
给定一个无重复元素的有序整数数组 nums 。
73+
74+
返回 恰好覆盖数组中所有数字 的 最小有序 区间范围列表。也就是说,nums 的每个元素都恰好被某个区间范围所覆盖,并且不存在属于某个范围但不属于 nums 的数字 x 。
75+
76+
列表中的每个区间范围 [a,b] 应该按如下格式输出:
77+
78+
- "a->b" ,如果 a != b
79+
- "a" ,如果 a == b
80+
81+
## 解题思路
82+
83+
- 简单题。按照题意,用一个游标变量累加寻找连续的区间。一旦出现了中断,就按照题意格式输出。输出的规则有多种,带箭头的区间,单个元素区间,空区间。
84+
85+
## 代码
86+
87+
```go
88+
package leetcode
89+
90+
import (
91+
"strconv"
92+
)
93+
94+
func summaryRanges(nums []int) (ans []string) {
95+
for i, n := 0, len(nums); i < n; {
96+
left := i
97+
for i++; i < n && nums[i-1]+1 == nums[i]; i++ {
98+
}
99+
s := strconv.Itoa(nums[left])
100+
if left != i-1 {
101+
s += "->" + strconv.Itoa(nums[i-1])
102+
}
103+
ans = append(ans, s)
104+
}
105+
return
106+
}
107+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package leetcode
2+
3+
import (
4+
"strings"
5+
)
6+
7+
func reformatNumber(number string) string {
8+
parts, nums := []string{}, []rune{}
9+
for _, r := range number {
10+
if r != '-' && r != ' ' {
11+
nums = append(nums, r)
12+
}
13+
}
14+
threeDigits, twoDigits := len(nums)/3, 0
15+
switch len(nums) % 3 {
16+
case 1:
17+
threeDigits--
18+
twoDigits = 2
19+
case 2:
20+
twoDigits = 1
21+
default:
22+
twoDigits = 0
23+
}
24+
for i := 0; i < threeDigits; i++ {
25+
s := ""
26+
s += string(nums[0:3])
27+
nums = nums[3:]
28+
parts = append(parts, s)
29+
}
30+
for i := 0; i < twoDigits; i++ {
31+
s := ""
32+
s += string(nums[0:2])
33+
nums = nums[2:]
34+
parts = append(parts, s)
35+
}
36+
return strings.Join(parts, "-")
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
type question1694 struct {
9+
para1694
10+
ans1694
11+
}
12+
13+
// para 是参数
14+
// one 代表第一个参数
15+
type para1694 struct {
16+
number string
17+
}
18+
19+
// ans 是答案
20+
// one 代表第一个答案
21+
type ans1694 struct {
22+
one string
23+
}
24+
25+
func Test_Problem1694(t *testing.T) {
26+
27+
qs := []question1694{
28+
29+
{
30+
para1694{"1-23-45 6"},
31+
ans1694{"123-456"},
32+
},
33+
34+
{
35+
para1694{"123 4-567"},
36+
ans1694{"123-45-67"},
37+
},
38+
39+
{
40+
para1694{"123 4-5678"},
41+
ans1694{"123-456-78"},
42+
},
43+
44+
{
45+
para1694{"12"},
46+
ans1694{"12"},
47+
},
48+
49+
{
50+
para1694{"--17-5 229 35-39475 "},
51+
ans1694{"175-229-353-94-75"},
52+
},
53+
54+
{
55+
para1694{"9964-"},
56+
ans1694{"99-64"},
57+
},
58+
}
59+
60+
fmt.Printf("------------------------Leetcode Problem 1694------------------------\n")
61+
62+
for _, q := range qs {
63+
_, p := q.ans1694, q.para1694
64+
fmt.Printf("【input】:%v 【output】:%v\n", p, reformatNumber(p.number))
65+
}
66+
fmt.Printf("\n\n\n")
67+
}

0 commit comments

Comments
 (0)