Skip to content

Commit d5441b5

Browse files
authored
feat: update lc problems (doocs#2506)
1 parent 9b70ced commit d5441b5

File tree

64 files changed

+1156
-693
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+1156
-693
lines changed

solution/0100-0199/0161.One Edit Distance/README.md

+20-2
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050

5151
记 $m$ 表示字符串 $s$ 的长度,$n$ 表示字符串 $t$ 的长度。我们可以假定 $m$ 恒大于等于 $n$。
5252

53-
若 $m-n\gt1$,直接返回 false;
53+
若 $m-n \gt 1$,直接返回 false;
5454

5555
否则,遍历 $s$ 和 $t$,若遇到 $s[i]$ 不等于 $t[i]$:
5656

@@ -59,7 +59,7 @@
5959

6060
遍历结束,说明遍历过的 $s$ 跟 $t$ 所有字符相等,此时需要满足 $m=n+1$。
6161

62-
时间复杂度 $O(m)$,空间复杂度 $O(1)$。
62+
时间复杂度 $O(m)$,其中 $m$ 为字符串 $s$ 的长度。空间复杂度 $O(1)$。
6363

6464
<!-- tabs:start -->
6565

@@ -139,6 +139,24 @@ func isOneEditDistance(s string, t string) bool {
139139
}
140140
```
141141

142+
```ts
143+
function isOneEditDistance(s: string, t: string): boolean {
144+
const [m, n] = [s.length, t.length];
145+
if (m < n) {
146+
return isOneEditDistance(t, s);
147+
}
148+
if (m - n > 1) {
149+
return false;
150+
}
151+
for (let i = 0; i < n; ++i) {
152+
if (s[i] !== t[i]) {
153+
return s.slice(i + 1) === t.slice(i + (m === n ? 1 : 0));
154+
}
155+
}
156+
return m === n + 1;
157+
}
158+
```
159+
142160
<!-- tabs:end -->
143161

144162
<!-- end -->

solution/0100-0199/0161.One Edit Distance/README_EN.md

+32-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,20 @@
4343

4444
## Solutions
4545

46-
### Solution 1
46+
### Solution 1: Discuss Different Cases
47+
48+
Let $m$ represent the length of string $s$, and $n$ represent the length of string $t$. We can assume that $m$ is always greater than or equal to $n$.
49+
50+
If $m-n > 1$, return false directly;
51+
52+
Otherwise, iterate through $s$ and $t$, if $s[i]$ is not equal to $t[i]$:
53+
54+
- If $m \neq n$, compare $s[i+1:]$ with $t[i:]$, return true if they are equal, otherwise return false;
55+
- If $m = n$, compare $s[i:]$ with $t[i:]$, return true if they are equal, otherwise return false.
56+
57+
If the iteration ends, it means that all the characters of $s$ and $t$ that have been iterated are equal, at this time it needs to satisfy $m=n+1$.
58+
59+
The time complexity is $O(m)$, where $m$ is the length of string $s$. The space complexity is $O(1)$.
4760

4861
<!-- tabs:start -->
4962

@@ -123,6 +136,24 @@ func isOneEditDistance(s string, t string) bool {
123136
}
124137
```
125138

139+
```ts
140+
function isOneEditDistance(s: string, t: string): boolean {
141+
const [m, n] = [s.length, t.length];
142+
if (m < n) {
143+
return isOneEditDistance(t, s);
144+
}
145+
if (m - n > 1) {
146+
return false;
147+
}
148+
for (let i = 0; i < n; ++i) {
149+
if (s[i] !== t[i]) {
150+
return s.slice(i + 1) === t.slice(i + (m === n ? 1 : 0));
151+
}
152+
}
153+
return m === n + 1;
154+
}
155+
```
156+
126157
<!-- tabs:end -->
127158

128159
<!-- end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
function isOneEditDistance(s: string, t: string): boolean {
2+
const [m, n] = [s.length, t.length];
3+
if (m < n) {
4+
return isOneEditDistance(t, s);
5+
}
6+
if (m - n > 1) {
7+
return false;
8+
}
9+
for (let i = 0; i < n; ++i) {
10+
if (s[i] !== t[i]) {
11+
return s.slice(i + 1) === t.slice(i + (m === n ? 1 : 0));
12+
}
13+
}
14+
return m === n + 1;
15+
}

solution/0100-0199/0164.Maximum Gap/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ $$
5555

5656
遍历数组结束之后,每个非空的桶内的最小值和最大值都可以确定。按照桶的编号从小到大的顺序依次遍历每个桶,当前的桶的最小值和上一个非空的桶的最大值是排序后的相邻元素,计算两个相邻元素之差,并更新最大间距。遍历桶结束之后即可得到最大间距。
5757

58-
时间复杂度 $O(n)$,空间复杂度 $O(n)$。
58+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是数组 $nums$ 的长度。
5959

6060
<!-- tabs:start -->
6161

solution/0100-0199/0164.Maximum Gap/README_EN.md

+14-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,20 @@
3737

3838
## Solutions
3939

40-
### Solution 1
40+
### Solution 1: Discuss Different Cases
41+
42+
Let $m$ represent the length of string $s$, and $n$ represent the length of string $t$. We can assume that $m$ is always greater than or equal to $n$.
43+
44+
If $m-n > 1$, return false directly;
45+
46+
Otherwise, iterate through $s$ and $t$, if $s[i]$ is not equal to $t[i]$:
47+
48+
- If $m \neq n$, compare $s[i+1:]$ with $t[i:]$, return true if they are equal, otherwise return false;
49+
- If $m = n$, compare $s[i:]$ with $t[i:]$, return true if they are equal, otherwise return false.
50+
51+
If the iteration ends, it means that all the characters of $s$ and $t$ that have been iterated are equal, at this time it needs to satisfy $m=n+1$.
52+
53+
The time complexity is $O(m)$, where $m$ is the length of string $s$. The space complexity is $O(1)$.
4154

4255
<!-- tabs:start -->
4356

solution/0100-0199/0165.Compare Version Numbers/README.md

+10-7
Original file line numberDiff line numberDiff line change
@@ -159,13 +159,16 @@ func compareVersion(version1 string, version2 string) int {
159159

160160
```ts
161161
function compareVersion(version1: string, version2: string): number {
162-
let v1 = version1.split('.'),
163-
v2 = version2.split('.');
164-
for (let i = 0; i < Math.max(v1.length, v2.length); i++) {
165-
let c1 = Number(v1[i] || 0),
166-
c2 = Number(v2[i] || 0);
167-
if (c1 > c2) return 1;
168-
if (c1 < c2) return -1;
162+
const v1 = version1.split('.');
163+
const v2 = version2.split('.');
164+
for (let i = 0; i < Math.max(v1.length, v2.length); ++i) {
165+
const [n1, n2] = [+v1[i] || 0, +v2[i] || 0];
166+
if (n1 < n2) {
167+
return -1;
168+
}
169+
if (n1 > n2) {
170+
return 1;
171+
}
169172
}
170173
return 0;
171174
}

solution/0100-0199/0165.Compare Version Numbers/README_EN.md

+10-7
Original file line numberDiff line numberDiff line change
@@ -152,13 +152,16 @@ func compareVersion(version1 string, version2 string) int {
152152

153153
```ts
154154
function compareVersion(version1: string, version2: string): number {
155-
let v1 = version1.split('.'),
156-
v2 = version2.split('.');
157-
for (let i = 0; i < Math.max(v1.length, v2.length); i++) {
158-
let c1 = Number(v1[i] || 0),
159-
c2 = Number(v2[i] || 0);
160-
if (c1 > c2) return 1;
161-
if (c1 < c2) return -1;
155+
const v1 = version1.split('.');
156+
const v2 = version2.split('.');
157+
for (let i = 0; i < Math.max(v1.length, v2.length); ++i) {
158+
const [n1, n2] = [+v1[i] || 0, +v2[i] || 0];
159+
if (n1 < n2) {
160+
return -1;
161+
}
162+
if (n1 > n2) {
163+
return 1;
164+
}
162165
}
163166
return 0;
164167
}
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
function compareVersion(version1: string, version2: string): number {
2-
let v1 = version1.split('.'),
3-
v2 = version2.split('.');
4-
for (let i = 0; i < Math.max(v1.length, v2.length); i++) {
5-
let c1 = Number(v1[i] || 0),
6-
c2 = Number(v2[i] || 0);
7-
if (c1 > c2) return 1;
8-
if (c1 < c2) return -1;
2+
const v1 = version1.split('.');
3+
const v2 = version2.split('.');
4+
for (let i = 0; i < Math.max(v1.length, v2.length); ++i) {
5+
const [n1, n2] = [+v1[i] || 0, +v2[i] || 0];
6+
if (n1 < n2) {
7+
return -1;
8+
}
9+
if (n1 > n2) {
10+
return 1;
11+
}
912
}
1013
return 0;
1114
}

0 commit comments

Comments
 (0)