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 233d09c

Browse files
✨feat: Add 面试题 01.05
1 parent bd22fe8 commit 233d09c

File tree

3 files changed

+85
-0
lines changed

3 files changed

+85
-0
lines changed

‎Index/双指针.md‎

Lines changed: 1 addition & 0 deletions
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‎

Lines changed: 1 addition & 0 deletions
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

Lines changed: 83 additions & 0 deletions
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)

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