Skip to content

Commit 707cc92

Browse files
committed
feat: add solutions to lcci problem: No.10.01
No.10.01.Sorted Merge
1 parent 485db21 commit 707cc92

File tree

4 files changed

+120
-0
lines changed

4 files changed

+120
-0
lines changed

lcci/10.01.Sorted Merge/README.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,49 @@ var merge = function (A, m, B, n) {
7171
};
7272
```
7373

74+
### **TypeScript**
75+
76+
```ts
77+
/**
78+
Do not return anything, modify A in-place instead.
79+
*/
80+
function merge(A: number[], m: number, B: number[], n: number): void {
81+
for (let i = n + m - 1; i >= 0; i--) {
82+
const x = A[m - 1] ?? -Infinity;
83+
const y = B[n - 1] ?? -Infinity;
84+
if (x > y) {
85+
A[i] = x;
86+
m--;
87+
} else {
88+
A[i] = y;
89+
n--;
90+
}
91+
}
92+
}
93+
```
94+
95+
### **Rust**
96+
97+
```rust
98+
impl Solution {
99+
pub fn merge(a: &mut Vec<i32>, m: i32, b: &mut Vec<i32>, n: i32) {
100+
let mut m = m as usize;
101+
let mut n = n as usize;
102+
for i in (0..n + m).rev() {
103+
let x = if m != 0 { a[m - 1] } else { i32::MIN };
104+
let y = if n != 0 { b[n - 1] } else { i32::MIN };
105+
if x > y {
106+
a[i] = x;
107+
m -= 1;
108+
} else {
109+
a[i] = y;
110+
n -= 1;
111+
}
112+
}
113+
}
114+
}
115+
```
116+
74117
### **...**
75118

76119
```

lcci/10.01.Sorted Merge/README_EN.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,50 @@ var merge = function (A, m, B, n) {
6464
};
6565
```
6666

67+
### **TypeScript**
68+
69+
```ts
70+
/**
71+
Do not return anything, modify A in-place instead.
72+
*/
73+
function merge(A: number[], m: number, B: number[], n: number): void {
74+
for (let i = n + m - 1; i >= 0; i--) {
75+
const x = A[m - 1] ?? -Infinity;
76+
const y = B[n - 1] ?? -Infinity;
77+
if (x > y) {
78+
A[i] = x;
79+
m--;
80+
} else {
81+
A[i] = y;
82+
n--;
83+
}
84+
}
85+
}
86+
```
87+
88+
### **Rust**
89+
90+
```rust
91+
impl Solution {
92+
pub fn merge(a: &mut Vec<i32>, m: i32, b: &mut Vec<i32>, n: i32) {
93+
let mut m = m as usize;
94+
let mut n = n as usize;
95+
for i in (0..n + m).rev() {
96+
let x = if m != 0 { a[m - 1] } else { i32::MIN };
97+
let y = if n != 0 { b[n - 1] } else { i32::MIN };
98+
if x > y {
99+
a[i] = x;
100+
m -= 1;
101+
} else {
102+
a[i] = y;
103+
n -= 1;
104+
}
105+
}
106+
}
107+
}
108+
```
109+
110+
67111
### **...**
68112

69113
```

lcci/10.01.Sorted Merge/Solution.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
impl Solution {
2+
pub fn merge(a: &mut Vec<i32>, m: i32, b: &mut Vec<i32>, n: i32) {
3+
let mut m = m as usize;
4+
let mut n = n as usize;
5+
for i in (0..n + m).rev() {
6+
let x = if m != 0 { a[m - 1] } else { i32::MIN };
7+
let y = if n != 0 { b[n - 1] } else { i32::MIN };
8+
if x > y {
9+
a[i] = x;
10+
m -= 1;
11+
} else {
12+
a[i] = y;
13+
n -= 1;
14+
}
15+
}
16+
}
17+
}

lcci/10.01.Sorted Merge/Solution.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
Do not return anything, modify A in-place instead.
3+
*/
4+
function merge(A: number[], m: number, B: number[], n: number): void {
5+
for (let i = n + m - 1; i >= 0; i--) {
6+
const x = A[m - 1] ?? -Infinity;
7+
const y = B[n - 1] ?? -Infinity;
8+
if (x > y) {
9+
A[i] = x;
10+
m--;
11+
} else {
12+
A[i] = y;
13+
n--;
14+
}
15+
}
16+
}

0 commit comments

Comments
 (0)