Skip to content

Commit 51a5f9f

Browse files
Merge pull request SharingSource#515 from SharingSource/ac_oier
✨style: Bulk processing format
2 parents fc644c3 + 233d09c commit 51a5f9f

9 files changed

+94
-97
lines changed

Index/双指针.md

+1
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,5 @@
4747
| [2000. 反转单词前缀](https://leetcode-cn.com/problems/reverse-prefix-of-word/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/reverse-prefix-of-word/solution/gong-shui-san-xie-jian-dan-shuang-zhi-zh-dp9u/) | 简单 | 🤩🤩🤩🤩 |
4848
| [2024. 考试的最大困扰度](https://leetcode-cn.com/problems/maximize-the-confusion-of-an-exam/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/maximize-the-confusion-of-an-exam/solution/by-ac_oier-2rii/) | 中等 | 🤩🤩🤩🤩 |
4949
| [2047. 句子中的有效单词数](https://leetcode-cn.com/problems/number-of-valid-words-in-a-sentence/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/number-of-valid-words-in-a-sentence/solution/gong-shui-san-xie-jian-dan-zi-fu-chuan-m-5pcz/) | 简单 | 🤩🤩🤩🤩 |
50+
| [面试题 01.05. 一次编辑](https://leetcode.cn/problems/one-away-lcci/) | [LeetCode 题解链接](https://leetcode.cn/problems/one-away-lcci/solution/by-ac_oier-7ml0/) | 中等 | 🤩🤩🤩🤩 |
5051

Index/模拟.md

+1
Original file line numberDiff line numberDiff line change
@@ -153,5 +153,6 @@
153153
| [2043. 简易银行系统](https://leetcode-cn.com/problems/simple-bank-system/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/simple-bank-system/solution/by-ac_oier-9pqi/) | 中等 | 🤩🤩🤩🤩 |
154154
| [2047. 句子中的有效单词数](https://leetcode-cn.com/problems/number-of-valid-words-in-a-sentence/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/number-of-valid-words-in-a-sentence/solution/gong-shui-san-xie-jian-dan-zi-fu-chuan-m-5pcz/) | 简单 | 🤩🤩🤩🤩 |
155155
| [2069. 模拟行走机器人 II](https://leetcode-cn.com/problems/walking-robot-simulation-ii/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/walking-robot-simulation-ii/solution/by-ac_oier-6zib/) | 中等 | 🤩🤩🤩🤩 |
156+
| [面试题 01.05. 一次编辑](https://leetcode.cn/problems/one-away-lcci/) | [LeetCode 题解链接](https://leetcode.cn/problems/one-away-lcci/solution/by-ac_oier-7ml0/) | 中等 | 🤩🤩🤩🤩 |
156157
| [面试题 10.02. 变位词组](https://leetcode-cn.com/problems/group-anagrams-lcci/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/group-anagrams-lcci/solution/gong-shui-san-xie-tong-ji-bian-wei-ci-de-0iqe/) | 中等 | 🤩🤩🤩🤩 |
157158

LeetCode/171-180/172. 阶乘后的零(中等).md

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Tag : 「数学」
88

99
给定一个整数 $n$ ,返回 $n!$ 结果中尾随零的数量。
1010

11-
提示 $n! = n * (n - 1) * (n - 2) * ... * 3 * 2 * 1$
11+
提示 `n! = n * (n - 1) * (n - 2) * ... * 3 * 2 * 1`
1212

1313
示例 1:
1414
```

LeetCode/2051-2060/2055. 蜡烛之间的盘子(中等).md

+1-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ class Solution {
9595
}
9696
}
9797
```
98-
* 时间复杂度:令 `s` 的长度为 $n$,`qs` 长度为 $m$。统计所有的蜡烛并存入 `list` 的复杂度为 $O(n)$;预处理前缀和数组复杂度为 $O(n)$;处理单个询问需要进行两次二分(蜡烛数量最多为 $n$),复杂度为 $O(\log{n})$,共有 $m$ 个询问需要处理,复杂度为 $O(m * \log{n})$。整体复杂度为 $O(n + m\log{n})$
98+
* 时间复杂度:令 `s` 的长度为 $n$,`qs` 长度为 $m$。统计所有的蜡烛并存入 `list` 的复杂度为 $O(n)$;预处理前缀和数组复杂度为 $O(n)$;处理单个询问需要进行两次二分(蜡烛数量最多为 $n$),复杂度为 $O(\log{n})$,共有 $m$ 个询问需要处理,复杂度为 $O(m \times \log{n})$。整体复杂度为 $O(n + m\log{n})$
9999
* 空间复杂度:$O(n)$
100100

101101
---

LeetCode/501-510/590. N 叉树的后序遍历(简单).md

-88
This file was deleted.

LeetCode/661-670/661. 图片平滑器(简单).md

+4-4
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,8 @@ class Solution:
9494
ans[i][j] = tot // cnt
9595
return ans
9696
```
97-
* 时间复杂度:$O(m * n * C)$,其中 $C$ 为灰度单位所包含的单元格数量,固定为 $9$
98-
* 空间复杂度:$O(m * n)$
97+
* 时间复杂度:$O(m \times n \times C)$,其中 $C$ 为灰度单位所包含的单元格数量,固定为 $9$
98+
* 空间复杂度:$O(m \times n)$
9999

100100
---
101101

@@ -151,8 +151,8 @@ class Solution:
151151
ans[i][j] = tot // cnt
152152
return ans
153153
```
154-
* 时间复杂度:$O(m * n)$
155-
* 空间复杂度:$O(m * n)$
154+
* 时间复杂度:$O(m \times n)$
155+
* 空间复杂度:$O(m \times n)$
156156

157157
---
158158

LeetCode/681-690/688. 骑士在棋盘上的概率(中等).md

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Tag : 「线性 DP」
66

77

88

9-
在一个 $n x n$ 的国际象棋棋盘上,一个骑士从单元格 $(row, column)$ 开始,并尝试进行 $k$ 次移动。行和列是 从 $0$ 开始 的,所以左上单元格是 $(0,0)$ ,右下单元格是 $(n - 1, n - 1)$ 。
9+
在一个 $n \times n$ 的国际象棋棋盘上,一个骑士从单元格 $(row, column)$ 开始,并尝试进行 $k$ 次移动。行和列是 从 $0$ 开始 的,所以左上单元格是 $(0,0)$ ,右下单元格是 $(n - 1, n - 1)$ 。
1010

1111
象棋骑士有 $8$ 种可能的走法,如下图所示。每次移动在基本方向上是两个单元格,然后在正交方向上是一个单元格。
1212

@@ -83,8 +83,8 @@ class Solution {
8383
}
8484
}
8585
```
86-
* 时间复杂度:令某个位置可联通的格子数量 $C = 8$,复杂度为 $O(n^2 * k * C)$
87-
* 空间复杂度:$O(n^2 * k)$
86+
* 时间复杂度:令某个位置可联通的格子数量 $C = 8$,复杂度为 $O(n^2 \times k \times C)$
87+
* 空间复杂度:$O(n^2 \times k)$
8888

8989
---
9090

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
### 题目描述
2+
3+
这是 LeetCode 上的 **[面试题 01.05. 一次编辑](https://leetcode.cn/problems/one-away-lcci/solution/by-ac_oier-7ml0/)** ,难度为 **中等**
4+
5+
Tag : 「模拟」、「双指针」
6+
7+
8+
9+
字符串有三种编辑操作:插入一个字符、删除一个字符或者替换一个字符。 给定两个字符串,编写一个函数判定它们是否只需要一次(或者零次)编辑。
10+
11+
示例 1:
12+
```
13+
输入:
14+
first = "pale"
15+
second = "ple"
16+
17+
输出: True
18+
```
19+
示例 2:
20+
```
21+
输入:
22+
first = "pales"
23+
second = "pal"
24+
25+
输出: False
26+
```
27+
28+
---
29+
30+
### 双指针模拟
31+
32+
为了方便,我们令 $a = first$、$b = second$,两者长度为 $n$ 和 $m$,并让 $a$ 为两种中的长度较短的那个(若 $b$ 较短,则将两者交换)。
33+
34+
接下来是简单的双指针处理(使用 $cnt$ 记录操作次数):
35+
36+
1. 我们最多使用不超过一次的操作,因此如果 $\left | n - m \right | > 1$,直接返回 `False`
37+
2. 若两者长度差不超过 $1$,使用 $i$ 和 $j$ 分别指向两字符的最左侧进行诸位检查:
38+
* 若 $a[i] = b[j]$,让 $i$ 和 $j$ 继续后移进行检查;
39+
* 若 $a[i] \neq b[j]$,根据两字符串长度进行分情况讨论:
40+
* 若 $n = m$,说明此时只能通过「替换」操作消除不同,分别让 $i$ 和 $j$ 后移,并对 $cnt$ 进行加一操作;
41+
* 若 $n \neq m$,由于我们人为确保了 $a$ 更短,即此时是 $n < m$,此时只能通过对 $a$ 字符串进行「添加」操作来消除不同,此时让 $j$ 后移,$i$ 不动(含义为在 $a$ 字符串中的 $i$ 位置增加一个 $b[j]$ 字符),并对 $cnt$ 进行加一操作。
42+
43+
最终我们根据 $cnt$ 是否不超过 $1$ 来返回结果。
44+
45+
代码:
46+
```Java
47+
class Solution {
48+
public boolean oneEditAway(String a, String b) {
49+
int n = a.length(), m = b.length();
50+
if (Math.abs(n - m) > 1) return false;
51+
if (n > m) return oneEditAway(b, a);
52+
int i = 0, j = 0, cnt = 0;
53+
while (i < n && j < m && cnt <= 1) {
54+
char c1 = a.charAt(i), c2 = b.charAt(j);
55+
if (c1 == c2) {
56+
i++; j++;
57+
} else {
58+
if (n == m) {
59+
i++; j++; cnt++;
60+
} else {
61+
j++; cnt++;
62+
}
63+
}
64+
}
65+
return cnt <= 1;
66+
}
67+
}
68+
```
69+
* 时间复杂度:令 $n$ 和 $m$ 分别为两字符串长度,复杂度为 $O(\max(n, m))$
70+
* 空间复杂度:$O(1)$
71+
72+
---
73+
74+
### 最后
75+
76+
这是我们「刷穿 LeetCode」系列文章的第 `No.面试题 01.05` 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
77+
78+
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
79+
80+
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode
81+
82+
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
83+

0 commit comments

Comments
 (0)