Skip to content
This repository was archived by the owner on Sep 20, 2023. It is now read-only.

Commit 5548047

Browse files
aQuaaQua
authored andcommitted
821 added
1 parent fad64d8 commit 5548047

File tree

4 files changed

+127
-22
lines changed

4 files changed

+127
-22
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# [821. Shortest Distance to a Character](https://leetcode.com/problems/shortest-distance-to-a-character/)
2+
3+
## 题目
4+
5+
Given a string S and a character C, return an array of integers representing the shortest distance from the character C in the string.
6+
7+
Example 1:
8+
9+
```text
10+
Input: S = "loveleetcode", C = 'e'
11+
Output: [3, 2, 1, 0, 1, 0, 0, 1, 2, 2, 1, 0]
12+
```
13+
14+
Note:
15+
16+
1. S string length is in [1, 10000].
17+
1. C is a single character, and guaranteed to be in string S.
18+
1. All letters in S and C are lowercase.
19+
20+
## 解题思路
21+
22+
见程序注释
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package problem0821
2+
3+
func shortestToChar(S string, C byte) []int {
4+
res := make([]int, len(S))
5+
left, right := -len(S), next(-1, C, S)
6+
for i := range S {
7+
if i > right {
8+
left = right
9+
right = next(right, C, S)
10+
}
11+
12+
res[i] = min(i-left, right-i)
13+
}
14+
15+
return res
16+
}
17+
18+
func next(right int, c byte, s string) int {
19+
idx := right + 1
20+
for idx < len(s) {
21+
if s[idx] == c {
22+
return idx
23+
}
24+
idx++
25+
}
26+
// 由于 s[right+1:] 中不再含有 c
27+
// 返回的 idx 需要达到 2*len(s)
28+
// 来确保后续的 min 操作的正确性
29+
return len(s) * 2
30+
}
31+
32+
func min(a, b int) int {
33+
if a < b {
34+
return a
35+
}
36+
return b
37+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package problem0821
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
// tcs is testcase slice
11+
var tcs = []struct {
12+
S string
13+
C byte
14+
ans []int
15+
}{
16+
17+
{
18+
"abaa",
19+
'b',
20+
[]int{1, 0, 1, 2},
21+
},
22+
{
23+
"loveleetcode",
24+
'e',
25+
[]int{3, 2, 1, 0, 1, 0, 0, 1, 2, 2, 1, 0},
26+
},
27+
28+
// 可以有多个 testcase
29+
}
30+
31+
func Test_shortestToChar(t *testing.T) {
32+
ast := assert.New(t)
33+
34+
for _, tc := range tcs {
35+
fmt.Printf("~~%v~~\n", tc)
36+
ast.Equal(tc.ans, shortestToChar(tc.S, tc.C), "输入:%v", tc)
37+
}
38+
}
39+
40+
func Benchmark_shortestToChar(b *testing.B) {
41+
for i := 0; i < b.N; i++ {
42+
for _, tc := range tcs {
43+
shortestToChar(tc.S, tc.C)
44+
}
45+
}
46+
}

leetcode.json

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"Username": "aQuaYi",
3-
"Ranking": 1146,
4-
"Updated": "2018-05-07T11:48:33.782831775+08:00",
3+
"Ranking": 1130,
4+
"Updated": "2018-05-09T11:34:32.64432465+08:00",
55
"Record": {
66
"Easy": {
77
"Solved": 166,
@@ -1573,7 +1573,7 @@
15731573
"ID": 129,
15741574
"Title": "Sum Root to Leaf Numbers",
15751575
"TitleSlug": "sum-root-to-leaf-numbers",
1576-
"PassRate": "37%",
1576+
"PassRate": "38%",
15771577
"Difficulty": "Medium",
15781578
"IsAccepted": true,
15791579
"IsPaid": false,
@@ -5689,7 +5689,7 @@
56895689
"ID": 472,
56905690
"Title": "Concatenated Words",
56915691
"TitleSlug": "concatenated-words",
5692-
"PassRate": "30%",
5692+
"PassRate": "31%",
56935693
"Difficulty": "Hard",
56945694
"IsAccepted": true,
56955695
"IsPaid": false,
@@ -6421,7 +6421,7 @@
64216421
"ID": 533,
64226422
"Title": "Lonely Pixel II",
64236423
"TitleSlug": "lonely-pixel-ii",
6424-
"PassRate": "44%",
6424+
"PassRate": "45%",
64256425
"Difficulty": "Medium",
64266426
"IsAccepted": false,
64276427
"IsPaid": true,
@@ -6529,7 +6529,7 @@
65296529
"ID": 542,
65306530
"Title": "01 Matrix",
65316531
"TitleSlug": "01-matrix",
6532-
"PassRate": "33%",
6532+
"PassRate": "32%",
65336533
"Difficulty": "Medium",
65346534
"IsAccepted": true,
65356535
"IsPaid": false,
@@ -6685,7 +6685,7 @@
66856685
"ID": 555,
66866686
"Title": "Split Concatenated Strings",
66876687
"TitleSlug": "split-concatenated-strings",
6688-
"PassRate": "37%",
6688+
"PassRate": "38%",
66896689
"Difficulty": "Medium",
66906690
"IsAccepted": false,
66916691
"IsPaid": true,
@@ -7009,7 +7009,7 @@
70097009
"ID": 582,
70107010
"Title": "Kill Process",
70117011
"TitleSlug": "kill-process",
7012-
"PassRate": "50%",
7012+
"PassRate": "51%",
70137013
"Difficulty": "Medium",
70147014
"IsAccepted": false,
70157015
"IsPaid": true,
@@ -7081,7 +7081,7 @@
70817081
"ID": 588,
70827082
"Title": "Design In-Memory File System",
70837083
"TitleSlug": "design-in-memory-file-system",
7084-
"PassRate": "35%",
7084+
"PassRate": "36%",
70857085
"Difficulty": "Hard",
70867086
"IsAccepted": false,
70877087
"IsPaid": true,
@@ -7825,7 +7825,7 @@
78257825
"ID": 650,
78267826
"Title": "2 Keys Keyboard",
78277827
"TitleSlug": "2-keys-keyboard",
7828-
"PassRate": "44%",
7828+
"PassRate": "45%",
78297829
"Difficulty": "Medium",
78307830
"IsAccepted": true,
78317831
"IsPaid": false,
@@ -8137,7 +8137,7 @@
81378137
"ID": 676,
81388138
"Title": "Implement Magic Dictionary",
81398139
"TitleSlug": "implement-magic-dictionary",
8140-
"PassRate": "48%",
8140+
"PassRate": "49%",
81418141
"Difficulty": "Medium",
81428142
"IsAccepted": true,
81438143
"IsPaid": false,
@@ -8593,7 +8593,7 @@
85938593
"ID": 714,
85948594
"Title": "Best Time to Buy and Sell Stock with Transaction Fee",
85958595
"TitleSlug": "best-time-to-buy-and-sell-stock-with-transaction-fee",
8596-
"PassRate": "45%",
8596+
"PassRate": "46%",
85978597
"Difficulty": "Medium",
85988598
"IsAccepted": true,
85998599
"IsPaid": false,
@@ -8809,7 +8809,7 @@
88098809
"ID": 732,
88108810
"Title": "My Calendar III",
88118811
"TitleSlug": "my-calendar-iii",
8812-
"PassRate": "50%",
8812+
"PassRate": "49%",
88138813
"Difficulty": "Hard",
88148814
"IsAccepted": true,
88158815
"IsPaid": false,
@@ -9277,7 +9277,7 @@
92779277
"ID": 771,
92789278
"Title": "Jewels and Stones",
92799279
"TitleSlug": "jewels-and-stones",
9280-
"PassRate": "82%",
9280+
"PassRate": "81%",
92819281
"Difficulty": "Easy",
92829282
"IsAccepted": true,
92839283
"IsPaid": false,
@@ -9601,7 +9601,7 @@
96019601
"ID": 798,
96029602
"Title": "Smallest Rotation with Highest Score",
96039603
"TitleSlug": "smallest-rotation-with-highest-score",
9604-
"PassRate": "31%",
9604+
"PassRate": "32%",
96059605
"Difficulty": "Hard",
96069606
"IsAccepted": true,
96079607
"IsPaid": false,
@@ -9697,7 +9697,7 @@
96979697
"ID": 806,
96989698
"Title": "Number of Lines To Write String",
96999699
"TitleSlug": "number-of-lines-to-write-string",
9700-
"PassRate": "64%",
9700+
"PassRate": "63%",
97019701
"Difficulty": "Easy",
97029702
"IsAccepted": true,
97039703
"IsPaid": false,
@@ -9709,7 +9709,7 @@
97099709
"ID": 807,
97109710
"Title": "Max Increase to Keep City Skyline",
97119711
"TitleSlug": "max-increase-to-keep-city-skyline",
9712-
"PassRate": "82%",
9712+
"PassRate": "81%",
97139713
"Difficulty": "Medium",
97149714
"IsAccepted": true,
97159715
"IsPaid": false,
@@ -9805,7 +9805,7 @@
98059805
"ID": 815,
98069806
"Title": "Bus Routes",
98079807
"TitleSlug": "bus-routes",
9808-
"PassRate": "32%",
9808+
"PassRate": "31%",
98099809
"Difficulty": "Hard",
98109810
"IsAccepted": true,
98119811
"IsPaid": false,
@@ -9865,7 +9865,7 @@
98659865
"ID": 820,
98669866
"Title": "Short Encoding of Words",
98679867
"TitleSlug": "short-encoding-of-words",
9868-
"PassRate": "39%",
9868+
"PassRate": "40%",
98699869
"Difficulty": "Medium",
98709870
"IsAccepted": false,
98719871
"IsPaid": false,
@@ -9961,7 +9961,7 @@
99619961
"ID": 828,
99629962
"Title": "Unique Letter String",
99639963
"TitleSlug": "unique-letter-string",
9964-
"PassRate": "28%",
9964+
"PassRate": "31%",
99659965
"Difficulty": "Hard",
99669966
"IsAccepted": false,
99679967
"IsPaid": false,
@@ -9973,7 +9973,7 @@
99739973
"ID": 829,
99749974
"Title": "Consecutive Numbers Sum",
99759975
"TitleSlug": "consecutive-numbers-sum",
9976-
"PassRate": "23%",
9976+
"PassRate": "25%",
99779977
"Difficulty": "Medium",
99789978
"IsAccepted": false,
99799979
"IsPaid": false,
@@ -9985,7 +9985,7 @@
99859985
"ID": 830,
99869986
"Title": "Positions of Large Groups",
99879987
"TitleSlug": "positions-of-large-groups",
9988-
"PassRate": "52%",
9988+
"PassRate": "51%",
99899989
"Difficulty": "Easy",
99909990
"IsAccepted": false,
99919991
"IsPaid": false,

0 commit comments

Comments
 (0)