diff --git a/Index/BFS.md b/Index/BFS.md index 951cae2a..76d30a58 100644 --- a/Index/BFS.md +++ b/Index/BFS.md @@ -11,6 +11,7 @@ | [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/) | 简单 | 🤩🤩🤩 | | [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/) | 困难 | 🤩🤩🤩 | | [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/) | 简单 | 🤩🤩🤩 | +| [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/) | 简单 | 🤩🤩🤩🤩 | | [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/) | 中等 | 🤩🤩🤩🤩 | | [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/) | 简单 | 🤩🤩🤩 | | [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/) | 简单 | 🤩🤩 | diff --git "a/Index/\346\250\241\346\213\237.md" "b/Index/\346\250\241\346\213\237.md" index 18051b4e..8ecdf824 100644 --- "a/Index/\346\250\241\346\213\237.md" +++ "b/Index/\346\250\241\346\213\237.md" @@ -90,6 +90,7 @@ | [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/) | 简单 | 🤩🤩🤩 | | [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/) | 简单 | 🤩🤩🤩🤩 | | [819. 最常见的单词](https://leetcode-cn.com/problems/most-common-word/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/most-common-word/solution/by-ac_oier-6aqd/) | 简单 | 🤩🤩🤩🤩 | +| [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/) | 简单 | 🤩🤩🤩🤩 | | [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/) | 中等 | 🤩🤩🤩 | | [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/) | 简单 | 🤩🤩🤩🤩🤩 | | [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/) | 简单 | 🤩🤩🤩🤩 | diff --git "a/LeetCode/821-830/821. \345\255\227\347\254\246\347\232\204\346\234\200\347\237\255\350\267\235\347\246\273\357\274\210\347\256\200\345\215\225\357\274\211.md" "b/LeetCode/821-830/821. \345\255\227\347\254\246\347\232\204\346\234\200\347\237\255\350\267\235\347\246\273\357\274\210\347\256\200\345\215\225\357\274\211.md" new file mode 100644 index 00000000..7c9a88f7 --- /dev/null +++ "b/LeetCode/821-830/821. \345\255\227\347\254\246\347\232\204\346\234\200\347\237\255\350\267\235\347\246\273\357\274\210\347\256\200\345\215\225\357\274\211.md" @@ -0,0 +1,116 @@ +### 题目描述 + +这是 LeetCode 上的 **[821. 字符的最短距离](https://leetcode-cn.com/problems/shortest-distance-to-a-character/solution/by-ac_oier-5bjs/)** ,难度为 **简单**。 + +Tag : 「模拟」、「BFS」 + + + +给你一个字符串 `s` 和一个字符 `c` ,且 `c` 是 `s` 中出现过的字符。 + +返回一个整数数组 `answer`,其中 $answer.length == s.length$ 且 $answer[i]$ 是 `s` 中从下标 $i$ 到离它 最近 的字符 `c` 的 距离 。 + +两个下标 $i$ 和 $j$ 之间的 距离 为 `abs(i - j)` ,其中 `abs` 是绝对值函数。 + +示例 1: +``` +输入:s = "loveleetcode", c = "e" + +输出:[3,2,1,0,1,0,0,1,2,2,1,0] + +解释:字符 'e' 出现在下标 3、5、6 和 11 处(下标从 0 开始计数)。 +距下标 0 最近的 'e' 出现在下标 3 ,所以距离为 abs(0 - 3) = 3 。 +距下标 1 最近的 'e' 出现在下标 3 ,所以距离为 abs(1 - 3) = 2 。 +对于下标 4 ,出现在下标 3 和下标 5 处的 'e' 都离它最近,但距离是一样的 abs(4 - 3) == abs(4 - 5) = 1 。 +距下标 8 最近的 'e' 出现在下标 6 ,所以距离为 abs(8 - 6) = 2 。 +``` +示例 2: +``` +输入:s = "aaab", c = "b" + +输出:[3,2,1,0] +``` + +提示: +* $1 <= s.length <= 10^4$ +* $s[i]$ 和 `c` 均为小写英文字母 +* 题目数据保证 `c` 在 `s` 中至少出现一次 + +--- + +### 遍历 + +根据题意进行模拟即可:两次遍历,第一次找到每个 $i$ 左边最近的 `c`,第二次找到每个 $i$ 右边最近的 `c`。 + +代码: +```Java +class Solution { + public int[] shortestToChar(String s, char c) { + int n = s.length(); + int[] ans = new int[n]; + Arrays.fill(ans, n + 1); + for (int i = 0, j = -1; i < n; i++) { + if (s.charAt(i) == c) j = i; + if (j != -1) ans[i] = i - j; + } + for (int i = n - 1, j = -1; i >= 0; i--) { + if (s.charAt(i) == c) j = i; + if (j != -1) ans[i] = Math.min(ans[i], j - i); + } + return ans; + } +} +``` +* 时间复杂度:$O(n)$ +* 空间复杂度:$O(1)$ + +--- + +### BFS + +起始令所有的 $ans[i] = -1$,然后将所有的 `c` 字符的下标入队,并更新 $ans[i] = 0$,然后跑一遍 `BFS` 逻辑,通过 $ans[i]$ 是否为 $-1$ 来判断是否重复入队。 + +代码: +```Java +class Solution { + public int[] shortestToChar(String s, char c) { + int n = s.length(); + int[] ans = new int[n]; + Arrays.fill(ans, -1); + Deque d = new ArrayDeque<>(); + for (int i = 0; i < n; i++) { + if (s.charAt(i) == c) { + d.addLast(i); + ans[i] = 0; + } + } + int[] dirs = new int[]{-1, 1}; + while (!d.isEmpty()) { + int t = d.pollFirst(); + for (int di : dirs) { + int ne = t + di; + if (ne >= 0 && ne < n && ans[ne] == -1) { + ans[ne] = ans[t] + 1; + d.addLast(ne); + } + } + } + return ans; + } +} +``` +* 时间复杂度:$O(n)$ +* 空间复杂度:$O(n)$ + +--- + +### 最后 + +这是我们「刷穿 LeetCode」系列文章的第 `No.821` 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。 + +在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。 + +为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode 。 + +在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。 +