Skip to content

feat: update lc problems #2506

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions solution/0100-0199/0161.One Edit Distance/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@

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

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

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

Expand All @@ -59,7 +59,7 @@

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

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

<!-- tabs:start -->

Expand Down Expand Up @@ -139,6 +139,24 @@ func isOneEditDistance(s string, t string) bool {
}
```

```ts
function isOneEditDistance(s: string, t: string): boolean {
const [m, n] = [s.length, t.length];
if (m < n) {
return isOneEditDistance(t, s);
}
if (m - n > 1) {
return false;
}
for (let i = 0; i < n; ++i) {
if (s[i] !== t[i]) {
return s.slice(i + 1) === t.slice(i + (m === n ? 1 : 0));
}
}
return m === n + 1;
}
```

<!-- tabs:end -->

<!-- end -->
33 changes: 32 additions & 1 deletion solution/0100-0199/0161.One Edit Distance/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,20 @@

## Solutions

### Solution 1
### Solution 1: Discuss Different Cases

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$.

If $m-n > 1$, return false directly;

Otherwise, iterate through $s$ and $t$, if $s[i]$ is not equal to $t[i]$:

- If $m \neq n$, compare $s[i+1:]$ with $t[i:]$, return true if they are equal, otherwise return false;
- If $m = n$, compare $s[i:]$ with $t[i:]$, return true if they are equal, otherwise return false.

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$.

The time complexity is $O(m)$, where $m$ is the length of string $s$. The space complexity is $O(1)$.

<!-- tabs:start -->

Expand Down Expand Up @@ -123,6 +136,24 @@ func isOneEditDistance(s string, t string) bool {
}
```

```ts
function isOneEditDistance(s: string, t: string): boolean {
const [m, n] = [s.length, t.length];
if (m < n) {
return isOneEditDistance(t, s);
}
if (m - n > 1) {
return false;
}
for (let i = 0; i < n; ++i) {
if (s[i] !== t[i]) {
return s.slice(i + 1) === t.slice(i + (m === n ? 1 : 0));
}
}
return m === n + 1;
}
```

<!-- tabs:end -->

<!-- end -->
15 changes: 15 additions & 0 deletions solution/0100-0199/0161.One Edit Distance/Solution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
function isOneEditDistance(s: string, t: string): boolean {
const [m, n] = [s.length, t.length];
if (m < n) {
return isOneEditDistance(t, s);
}
if (m - n > 1) {
return false;
}
for (let i = 0; i < n; ++i) {
if (s[i] !== t[i]) {
return s.slice(i + 1) === t.slice(i + (m === n ? 1 : 0));
}
}
return m === n + 1;
}
2 changes: 1 addition & 1 deletion solution/0100-0199/0164.Maximum Gap/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ $$

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

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

<!-- tabs:start -->

Expand Down
15 changes: 14 additions & 1 deletion solution/0100-0199/0164.Maximum Gap/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,20 @@

## Solutions

### Solution 1
### Solution 1: Discuss Different Cases

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$.

If $m-n > 1$, return false directly;

Otherwise, iterate through $s$ and $t$, if $s[i]$ is not equal to $t[i]$:

- If $m \neq n$, compare $s[i+1:]$ with $t[i:]$, return true if they are equal, otherwise return false;
- If $m = n$, compare $s[i:]$ with $t[i:]$, return true if they are equal, otherwise return false.

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$.

The time complexity is $O(m)$, where $m$ is the length of string $s$. The space complexity is $O(1)$.

<!-- tabs:start -->

Expand Down
17 changes: 10 additions & 7 deletions solution/0100-0199/0165.Compare Version Numbers/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,13 +159,16 @@ func compareVersion(version1 string, version2 string) int {

```ts
function compareVersion(version1: string, version2: string): number {
let v1 = version1.split('.'),
v2 = version2.split('.');
for (let i = 0; i < Math.max(v1.length, v2.length); i++) {
let c1 = Number(v1[i] || 0),
c2 = Number(v2[i] || 0);
if (c1 > c2) return 1;
if (c1 < c2) return -1;
const v1 = version1.split('.');
const v2 = version2.split('.');
for (let i = 0; i < Math.max(v1.length, v2.length); ++i) {
const [n1, n2] = [+v1[i] || 0, +v2[i] || 0];
if (n1 < n2) {
return -1;
}
if (n1 > n2) {
return 1;
}
}
return 0;
}
Expand Down
17 changes: 10 additions & 7 deletions solution/0100-0199/0165.Compare Version Numbers/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,13 +152,16 @@ func compareVersion(version1 string, version2 string) int {

```ts
function compareVersion(version1: string, version2: string): number {
let v1 = version1.split('.'),
v2 = version2.split('.');
for (let i = 0; i < Math.max(v1.length, v2.length); i++) {
let c1 = Number(v1[i] || 0),
c2 = Number(v2[i] || 0);
if (c1 > c2) return 1;
if (c1 < c2) return -1;
const v1 = version1.split('.');
const v2 = version2.split('.');
for (let i = 0; i < Math.max(v1.length, v2.length); ++i) {
const [n1, n2] = [+v1[i] || 0, +v2[i] || 0];
if (n1 < n2) {
return -1;
}
if (n1 > n2) {
return 1;
}
}
return 0;
}
Expand Down
17 changes: 10 additions & 7 deletions solution/0100-0199/0165.Compare Version Numbers/Solution.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
function compareVersion(version1: string, version2: string): number {
let v1 = version1.split('.'),
v2 = version2.split('.');
for (let i = 0; i < Math.max(v1.length, v2.length); i++) {
let c1 = Number(v1[i] || 0),
c2 = Number(v2[i] || 0);
if (c1 > c2) return 1;
if (c1 < c2) return -1;
const v1 = version1.split('.');
const v2 = version2.split('.');
for (let i = 0; i < Math.max(v1.length, v2.length); ++i) {
const [n1, n2] = [+v1[i] || 0, +v2[i] || 0];
if (n1 < n2) {
return -1;
}
if (n1 > n2) {
return 1;
}
}
return 0;
}
Loading