Skip to content

Commit 67fd98d

Browse files
committed
feat: add solutions to lc problem: No.0821
No.0821.Shortest Distance to a Character
1 parent 5509154 commit 67fd98d

File tree

6 files changed

+475
-0
lines changed

6 files changed

+475
-0
lines changed

solution/0800-0899/0821.Shortest Distance to a Character/README.md

Lines changed: 207 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,22 +46,112 @@
4646

4747
<!-- 这里可写通用的实现逻辑 -->
4848

49+
**方法一:两次遍历**
50+
51+
两次遍历,找出每个字符左侧和右侧最近的 c,算出最短距离。
52+
53+
**方法二:BFS**
54+
55+
在字符串 s 中找出所有字符 c 对应的下标,并放入队列 q。
56+
57+
BFS 向左右两边扩散,找出最短距离。
58+
4959
<!-- tabs:start -->
5060

5161
### **Python3**
5262

5363
<!-- 这里可写当前语言的特殊实现逻辑 -->
5464

5565
```python
66+
class Solution:
67+
def shortestToChar(self, s: str, c: str) -> List[int]:
68+
n = len(s)
69+
ans = [0] * n
70+
j = float('inf')
71+
for i, ch in enumerate(s):
72+
if ch == c:
73+
j = i
74+
ans[i] = abs(i - j)
75+
j = float('inf')
76+
for i in range(n - 1, -1, -1):
77+
if s[i] == c:
78+
j = i
79+
ans[i] = min(ans[i], abs(i - j))
80+
return ans
81+
```
5682

83+
```python
84+
class Solution:
85+
def shortestToChar(self, s: str, c: str) -> List[int]:
86+
q = deque([i for i, ch in enumerate(s) if ch == c])
87+
ans = [0 if ch == c else -1 for ch in s]
88+
d = 0
89+
while q:
90+
d += 1
91+
for _ in range(len(q)):
92+
i = q.popleft()
93+
for j in (i - 1, i + 1):
94+
if 0 <= j < len(s) and ans[j] == -1:
95+
ans[j] = d
96+
q.append(j)
97+
return ans
5798
```
5899

59100
### **Java**
60101

61102
<!-- 这里可写当前语言的特殊实现逻辑 -->
62103

63104
```java
105+
class Solution {
106+
public int[] shortestToChar(String s, char c) {
107+
int n = s.length();
108+
int[] ans = new int[n];
109+
for (int i = 0, j = Integer.MAX_VALUE; i < n; ++i) {
110+
if (s.charAt(i) == c) {
111+
j = i;
112+
}
113+
ans[i] = Math.abs(i - j);
114+
}
115+
for (int i = n - 1, j = Integer.MAX_VALUE; i >= 0; --i) {
116+
if (s.charAt(i) == c) {
117+
j = i;
118+
}
119+
ans[i] = Math.min(ans[i], Math.abs(i - j));
120+
}
121+
return ans;
122+
}
123+
}
124+
```
64125

126+
```java
127+
class Solution {
128+
public int[] shortestToChar(String s, char c) {
129+
Deque<Integer> q = new ArrayDeque<>();
130+
int n = s.length();
131+
int[] ans = new int[n];
132+
Arrays.fill(ans, -1);
133+
for (int i = 0; i < n; ++i) {
134+
if (s.charAt(i) == c) {
135+
q.offer(i);
136+
ans[i] = 0;
137+
}
138+
}
139+
int d = 0;
140+
while (!q.isEmpty()) {
141+
++d;
142+
for (int t = q.size(); t > 0; --t) {
143+
int i = q.poll();
144+
for (int j : Arrays.asList(i - 1, i + 1)) {
145+
if (j >= 0 && j < n && ans[j] == -1) {
146+
ans[j] = d;
147+
q.offer(j);
148+
}
149+
}
150+
}
151+
}
152+
return ans;
153+
}
154+
}
65155
```
66156

67157
### **TypeScript**
@@ -135,6 +225,123 @@ impl Solution {
135225
}
136226
```
137227

228+
### **C++**
229+
230+
```cpp
231+
class Solution {
232+
public:
233+
vector<int> shortestToChar(string s, char c) {
234+
int n = s.size();
235+
vector<int> ans(n);
236+
for (int i = 0, j = INT_MAX; i < n; ++i)
237+
{
238+
if (s[i] == c) j = i;
239+
ans[i] = abs(i - j);
240+
}
241+
for (int i = n - 1, j = INT_MAX; i >= 0; --i)
242+
{
243+
if (s[i] == c) j = i;
244+
ans[i] = min(ans[i], abs(i - j));
245+
}
246+
return ans;
247+
}
248+
};
249+
```
250+
251+
```cpp
252+
class Solution {
253+
public:
254+
vector<int> shortestToChar(string s, char c) {
255+
int n = s.size();
256+
vector<int> ans(n, -1);
257+
queue<int> q;
258+
for (int i = 0; i < n; ++i)
259+
{
260+
if (s[i] == c)
261+
{
262+
q.push(i);
263+
ans[i] = 0;
264+
}
265+
}
266+
int d = 0;
267+
while (!q.empty())
268+
{
269+
++d;
270+
for (int t = q.size(); t > 0; --t)
271+
{
272+
int i = q.front();
273+
q.pop();
274+
vector<int> dirs{i - 1, i + 1};
275+
for (int& j : dirs)
276+
{
277+
if (j >= 0 && j < n && ans[j] == -1)
278+
{
279+
ans[j] = d;
280+
q.push(j);
281+
}
282+
}
283+
}
284+
}
285+
return ans;
286+
}
287+
};
288+
```
289+
290+
### **Go**
291+
292+
```go
293+
func shortestToChar(s string, c byte) []int {
294+
n := len(s)
295+
ans := make([]int, n)
296+
for i, j := 0, -10000; i < n; i++ {
297+
if s[i] == c {
298+
j = i
299+
}
300+
ans[i] = i - j
301+
}
302+
for i, j := n-1, 10000; i >= 0; i-- {
303+
if s[i] == c {
304+
j = i
305+
}
306+
if j-i < ans[i] {
307+
ans[i] = j - i
308+
}
309+
}
310+
return ans
311+
}
312+
```
313+
314+
```go
315+
func shortestToChar(s string, c byte) []int {
316+
n := len(s)
317+
var q []int
318+
ans := make([]int, n)
319+
for i := range s {
320+
ans[i] = -1
321+
if s[i] == c {
322+
q = append(q, i)
323+
ans[i] = 0
324+
}
325+
}
326+
327+
d := 0
328+
for len(q) > 0 {
329+
d++
330+
for t := len(q); t > 0; t-- {
331+
i := q[0]
332+
q = q[1:]
333+
for _, j := range []int{i - 1, i + 1} {
334+
if j >= 0 && j < n && ans[j] == -1 {
335+
ans[j] = d
336+
q = append(q, j)
337+
}
338+
}
339+
}
340+
}
341+
return ans
342+
}
343+
```
344+
138345
### **...**
139346

140347
```

0 commit comments

Comments
 (0)