Skip to content

Commit 44de295

Browse files
committed
feat: add rust solution to lc problem: No.0088
No.0088.Merge Sorted Array
1 parent 8eca353 commit 44de295

File tree

3 files changed

+88
-0
lines changed

3 files changed

+88
-0
lines changed

solution/0000-0099/0088.Merge Sorted Array/README.md

+31
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,37 @@ var merge = function (nums1, m, nums2, n) {
148148
};
149149
```
150150

151+
### **Rust**
152+
153+
```rust
154+
impl Solution {
155+
pub fn merge(nums1: &mut Vec<i32>, m: i32, nums2: &mut Vec<i32>, n: i32) {
156+
let (mut m, mut n) = (m as usize, n as usize);
157+
for i in (0..m + n).rev() {
158+
nums1[i] = match (m == 0, n == 0) {
159+
(true, false) => {
160+
n -= 1;
161+
nums2[n]
162+
}
163+
(false, true) => {
164+
m -= 1;
165+
nums1[m]
166+
}
167+
(_, _) => {
168+
if nums1[m - 1] > nums2[n - 1] {
169+
m -= 1;
170+
nums1[m]
171+
} else {
172+
n -= 1;
173+
nums2[n]
174+
}
175+
}
176+
}
177+
}
178+
}
179+
}
180+
```
181+
151182
### **...**
152183

153184
```

solution/0000-0099/0088.Merge Sorted Array/README_EN.md

+31
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,37 @@ var merge = function (nums1, m, nums2, n) {
135135
};
136136
```
137137

138+
### **Rust**
139+
140+
```rust
141+
impl Solution {
142+
pub fn merge(nums1: &mut Vec<i32>, m: i32, nums2: &mut Vec<i32>, n: i32) {
143+
let (mut m, mut n) = (m as usize, n as usize);
144+
for i in (0..m + n).rev() {
145+
nums1[i] = match (m == 0, n == 0) {
146+
(true, false) => {
147+
n -= 1;
148+
nums2[n]
149+
}
150+
(false, true) => {
151+
m -= 1;
152+
nums1[m]
153+
}
154+
(_, _) => {
155+
if nums1[m - 1] > nums2[n - 1] {
156+
m -= 1;
157+
nums1[m]
158+
} else {
159+
n -= 1;
160+
nums2[n]
161+
}
162+
}
163+
}
164+
}
165+
}
166+
}
167+
```
168+
138169
### **...**
139170

140171
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
impl Solution {
2+
pub fn merge(nums1: &mut Vec<i32>, m: i32, nums2: &mut Vec<i32>, n: i32) {
3+
let (mut m, mut n) = (m as usize, n as usize);
4+
for i in (0..m + n).rev() {
5+
nums1[i] = match (m == 0, n == 0) {
6+
(true, false) => {
7+
n -= 1;
8+
nums2[n]
9+
}
10+
(false, true) => {
11+
m -= 1;
12+
nums1[m]
13+
}
14+
(_, _) => {
15+
if nums1[m - 1] > nums2[n - 1] {
16+
m -= 1;
17+
nums1[m]
18+
} else {
19+
n -= 1;
20+
nums2[n]
21+
}
22+
}
23+
}
24+
}
25+
}
26+
}

0 commit comments

Comments
 (0)