Skip to content

feat: update solutions #2258

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
Jan 24, 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
30 changes: 12 additions & 18 deletions solution/0000-0099/0088.Merge Sorted Array/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,18 @@ function merge(nums1: number[], m: number, nums2: number[], n: number): void {
}
```

```ts
/**
Do not return anything, modify nums1 in-place instead.
*/
function merge(nums1: number[], m: number, nums2: number[], n: number): void {
nums1.length = m;
nums2.length = n;
nums1.push(...nums2);
nums1.sort((a, b) => a - b);
}
```

```rust
impl Solution {
pub fn merge(nums1: &mut Vec<i32>, m: i32, nums2: &mut Vec<i32>, n: i32) {
Expand Down Expand Up @@ -198,22 +210,4 @@ class Solution {

<!-- tabs:end -->

### 方法二

<!-- tabs:start -->

```ts
/**
Do not return anything, modify nums1 in-place instead.
*/
function merge(nums1: number[], m: number, nums2: number[], n: number): void {
nums1.length = m;
nums2.length = n;
nums1.push(...nums2);
nums1.sort((a, b) => a - b);
}
```

<!-- tabs:end -->

<!-- end -->
30 changes: 12 additions & 18 deletions solution/0000-0099/0088.Merge Sorted Array/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,18 @@ function merge(nums1: number[], m: number, nums2: number[], n: number): void {
}
```

```ts
/**
Do not return anything, modify nums1 in-place instead.
*/
function merge(nums1: number[], m: number, nums2: number[], n: number): void {
nums1.length = m;
nums2.length = n;
nums1.push(...nums2);
nums1.sort((a, b) => a - b);
}
```

```rust
impl Solution {
pub fn merge(nums1: &mut Vec<i32>, m: i32, nums2: &mut Vec<i32>, n: i32) {
Expand Down Expand Up @@ -193,22 +205,4 @@ class Solution {

<!-- tabs:end -->

### Solution 2

<!-- tabs:start -->

```ts
/**
Do not return anything, modify nums1 in-place instead.
*/
function merge(nums1: number[], m: number, nums2: number[], n: number): void {
nums1.length = m;
nums2.length = n;
nums1.push(...nums2);
nums1.sort((a, b) => a - b);
}
```

<!-- tabs:end -->

<!-- end -->
4 changes: 1 addition & 3 deletions solution/0000-0099/0091.Decode Ways/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,6 @@

时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是字符串的长度。

我们注意到,状态 $f[i]$ 仅与状态 $f[i-1]$ 和状态 $f[i-2]$ 有关,而与其他状态无关,因此我们可以使用两个变量代替这两个状态,使得原来的空间复杂度 $O(n)$ 降低至 $O(1)$。

<!-- tabs:start -->

```python
Expand Down Expand Up @@ -187,7 +185,7 @@ public class Solution {

<!-- tabs:end -->

### 方法二
我们注意到,状态 $f[i]$ 仅与状态 $f[i-1]$ 和状态 $f[i-2]$ 有关,而与其他状态无关,因此我们可以使用两个变量代替这两个状态,使得原来的空间复杂度 $O(n)$ 降低至 $O(1)$。

<!-- tabs:start -->

Expand Down
2 changes: 1 addition & 1 deletion solution/0000-0099/0091.Decode Ways/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ public class Solution {

<!-- tabs:end -->

### Solution 2
We notice that the state $f[i]$ is only related to the states $f[i-1]$ and $f[i-2]$, and is irrelevant to other states. Therefore, we can use two variables to replace these two states, reducing the original space complexity from $O(n)$ to $O(1)$.

<!-- tabs:start -->

Expand Down
4 changes: 1 addition & 3 deletions solution/0000-0099/0097.Interleaving String/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -357,8 +357,6 @@ $$

时间复杂度 $O(m \times n)$,空间复杂度 $O(m \times n)$。其中 $m$ 和 $n$ 分别是字符串 $s_1$ 和 $s_2$ 的长度。

我们注意到,状态 $f[i][j]$ 只和状态 $f[i - 1][j]$、$f[i][j - 1]$、$f[i - 1][j - 1]$ 有关,因此我们可以使用滚动数组优化空间复杂度,将空间复杂度优化到 $O(n)$。

<!-- tabs:start -->

```python
Expand Down Expand Up @@ -508,7 +506,7 @@ public class Solution {

<!-- tabs:end -->

### 方法三
我们注意到,状态 $f[i][j]$ 只和状态 $f[i - 1][j]$、$f[i][j - 1]$、$f[i - 1][j - 1]$ 有关,因此我们可以使用滚动数组优化空间复杂度,将空间复杂度优化到 $O(n)$。

<!-- tabs:start -->

Expand Down
4 changes: 1 addition & 3 deletions solution/0000-0099/0097.Interleaving String/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -357,8 +357,6 @@ The answer is $f[m][n]$.

The time complexity is $O(m \times n)$, and the space complexity is $O(m \times n)$. Here, $m$ and $n$ are the lengths of strings $s_1$ and $s_2$ respectively.

We notice that the state $f[i][j]$ is only related to the states $f[i - 1][j]$, $f[i][j - 1]$, and $f[i - 1][j - 1]$. Therefore, we can use a rolling array to optimize the space complexity, reducing the original space complexity from $O(m \times n)$ to $O(n)$.

<!-- tabs:start -->

```python
Expand Down Expand Up @@ -508,7 +506,7 @@ public class Solution {

<!-- tabs:end -->

### Solution 3
We notice that the state $f[i][j]$ is only related to the states $f[i - 1][j]$, $f[i][j - 1]$, and $f[i - 1][j - 1]$. Therefore, we can use a rolling array to optimize the space complexity, reducing the original space complexity from $O(m \times n)$ to $O(n)$.

<!-- tabs:start -->

Expand Down