Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f0d84ba

Browse files
Merge pull request SharingSource#476 from SharingSource/ac_oier
✨feat: Add 821
2 parents 5037019 + a799b1c commit f0d84ba

File tree

3 files changed

+118
-0
lines changed

3 files changed

+118
-0
lines changed

‎Index/BFS.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
| [690. 员工的重要性](https://leetcode-cn.com/problems/employee-importance/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/employee-importance/solution/gong-shui-san-xie-yi-ti-shuang-jie-di-gu-s79x/) | 简单 | 🤩🤩🤩 |
1212
| [778. 水位上升的泳池中游泳](https://leetcode-cn.com/problems/swim-in-rising-water/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/swim-in-rising-water/solution/gong-shui-san-xie-yi-ti-shuang-jie-krusk-7c6o/) | 困难 | 🤩🤩🤩 |
1313
| [783. 二叉搜索树节点最小距离](https://leetcode-cn.com/problems/minimum-distance-between-bst-nodes/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/minimum-distance-between-bst-nodes/solution/gong-shui-san-xie-yi-ti-san-jie-shu-de-s-7r17/) | 简单 | 🤩🤩🤩 |
14+
| [821. 字符的最短距离](https://leetcode-cn.com/problems/shortest-distance-to-a-character/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/shortest-distance-to-a-character/solution/by-ac_oier-5bjs/) | 简单 | 🤩🤩🤩🤩 |
1415
| [838. 推多米诺](https://leetcode-cn.com/problems/push-dominoes/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/push-dominoes/solution/gong-shui-san-xie-yi-ti-shuang-jie-bfs-y-z52w/) | 中等 | 🤩🤩🤩🤩 |
1516
| [938. 二叉搜索树的范围和](https://leetcode-cn.com/problems/range-sum-of-bst/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/range-sum-of-bst/solution/gong-shui-san-xie-yi-ti-shuang-jie-di-gu-q2fo/) | 简单 | 🤩🤩🤩 |
1617
| [993. 二叉树的堂兄弟节点](https://leetcode-cn.com/problems/cousins-in-binary-tree/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/cousins-in-binary-tree/solution/gong-shui-san-xie-shu-de-sou-suo-dfs-bfs-b200/) | 简单 | 🤩🤩 |

‎Index/模拟.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@
9090
| [804. 唯一摩尔斯密码词](https://leetcode-cn.com/problems/unique-morse-code-words/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/unique-morse-code-words/solution/by-ac_oier-a9hv/) | 简单 | 🤩🤩🤩 |
9191
| [806. 写字符串需要的行数](https://leetcode-cn.com/problems/number-of-lines-to-write-string/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/number-of-lines-to-write-string/solution/by-ac_oier-5hg2/) | 简单 | 🤩🤩🤩🤩 |
9292
| [819. 最常见的单词](https://leetcode-cn.com/problems/most-common-word/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/most-common-word/solution/by-ac_oier-6aqd/) | 简单 | 🤩🤩🤩🤩 |
93+
| [821. 字符的最短距离](https://leetcode-cn.com/problems/shortest-distance-to-a-character/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/shortest-distance-to-a-character/solution/by-ac_oier-5bjs/) | 简单 | 🤩🤩🤩🤩 |
9394
| [846. 一手顺子](https://leetcode-cn.com/problems/hand-of-straights/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/hand-of-straights/solution/gong-shui-san-xie-shu-ju-jie-gou-mo-ni-t-4hxw/) | 中等 | 🤩🤩🤩 |
9495
| [859. 亲密字符串](https://leetcode-cn.com/problems/buddy-strings/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/buddy-strings/solution/gong-shui-san-xie-jian-dan-zi-fu-chuan-m-q056/) | 简单 | 🤩🤩🤩🤩🤩 |
9596
| [867. 转置矩阵](https://leetcode-cn.com/problems/transpose-matrix/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/transpose-matrix/solution/yi-you-wei-jin-huo-xu-ni-neng-kan-kan-zh-m53m/) | 简单 | 🤩🤩🤩🤩 |
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
### 题目描述
2+
3+
这是 LeetCode 上的 **[821. 字符的最短距离](https://leetcode-cn.com/problems/shortest-distance-to-a-character/solution/by-ac_oier-5bjs/)** ,难度为 **简单**
4+
5+
Tag : 「模拟」、「BFS」
6+
7+
8+
9+
给你一个字符串 `s` 和一个字符 `c` ,且 `c``s` 中出现过的字符。
10+
11+
返回一个整数数组 `answer`,其中 $answer.length == s.length$ 且 $answer[i]$ 是 `s` 中从下标 $i$ 到离它 最近 的字符 `c` 的 距离 。
12+
13+
两个下标 $i$ 和 $j$ 之间的 距离 为 `abs(i - j)` ,其中 `abs` 是绝对值函数。
14+
15+
示例 1:
16+
```
17+
输入:s = "loveleetcode", c = "e"
18+
19+
输出:[3,2,1,0,1,0,0,1,2,2,1,0]
20+
21+
解释:字符 'e' 出现在下标 3、5、6 和 11 处(下标从 0 开始计数)。
22+
距下标 0 最近的 'e' 出现在下标 3 ,所以距离为 abs(0 - 3) = 3 。
23+
距下标 1 最近的 'e' 出现在下标 3 ,所以距离为 abs(1 - 3) = 2 。
24+
对于下标 4 ,出现在下标 3 和下标 5 处的 'e' 都离它最近,但距离是一样的 abs(4 - 3) == abs(4 - 5) = 1 。
25+
距下标 8 最近的 'e' 出现在下标 6 ,所以距离为 abs(8 - 6) = 2 。
26+
```
27+
示例 2:
28+
```
29+
输入:s = "aaab", c = "b"
30+
31+
输出:[3,2,1,0]
32+
```
33+
34+
提示:
35+
* 1ドル <= s.length <= 10^4$
36+
* $s[i]$ 和 `c` 均为小写英文字母
37+
* 题目数据保证 `c``s` 中至少出现一次
38+
39+
---
40+
41+
### 遍历
42+
43+
根据题意进行模拟即可:两次遍历,第一次找到每个 $i$ 左边最近的 `c`,第二次找到每个 $i$ 右边最近的 `c`
44+
45+
代码:
46+
```Java
47+
class Solution {
48+
public int[] shortestToChar(String s, char c) {
49+
int n = s.length();
50+
int[] ans = new int[n];
51+
Arrays.fill(ans, n + 1);
52+
for (int i = 0, j = -1; i < n; i++) {
53+
if (s.charAt(i) == c) j = i;
54+
if (j != -1) ans[i] = i - j;
55+
}
56+
for (int i = n - 1, j = -1; i >= 0; i--) {
57+
if (s.charAt(i) == c) j = i;
58+
if (j != -1) ans[i] = Math.min(ans[i], j - i);
59+
}
60+
return ans;
61+
}
62+
}
63+
```
64+
* 时间复杂度:$O(n)$
65+
* 空间复杂度:$O(1)$
66+
67+
---
68+
69+
### BFS
70+
71+
起始令所有的 $ans[i] = -1,ドル然后将所有的 `c` 字符的下标入队,并更新 $ans[i] = 0,ドル然后跑一遍 `BFS` 逻辑,通过 $ans[i]$ 是否为 $-1$ 来判断是否重复入队。
72+
73+
代码:
74+
```Java
75+
class Solution {
76+
public int[] shortestToChar(String s, char c) {
77+
int n = s.length();
78+
int[] ans = new int[n];
79+
Arrays.fill(ans, -1);
80+
Deque<Integer> d = new ArrayDeque<>();
81+
for (int i = 0; i < n; i++) {
82+
if (s.charAt(i) == c) {
83+
d.addLast(i);
84+
ans[i] = 0;
85+
}
86+
}
87+
int[] dirs = new int[]{-1, 1};
88+
while (!d.isEmpty()) {
89+
int t = d.pollFirst();
90+
for (int di : dirs) {
91+
int ne = t + di;
92+
if (ne >= 0 && ne < n && ans[ne] == -1) {
93+
ans[ne] = ans[t] + 1;
94+
d.addLast(ne);
95+
}
96+
}
97+
}
98+
return ans;
99+
}
100+
}
101+
```
102+
* 时间复杂度:$O(n)$
103+
* 空间复杂度:$O(n)$
104+
105+
---
106+
107+
### 最后
108+
109+
这是我们「刷穿 LeetCode」系列文章的第 `No.821` 篇,系列开始于 2021年01月01日,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
110+
111+
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
112+
113+
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode
114+
115+
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
116+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /