-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
37 lines (37 loc) · 894 Bytes
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
export default function merge(
nums1: number[],
m: number,
nums2: number[],
n: number,
): void {
if (n === 0) {
return;
}
if (m == 0) {
nums1.splice(0, n, ...nums2);
return;
}
const temp = nums1.slice(0, m);
nums1.length = 0;
while (temp.length > 0 && nums2.length > 0) {
if (temp[0] > nums2[0]) {
const first2 = nums2.shift();
if (typeof first2 === "undefined") {
throw Error("Invalid number");
}
nums1.push(first2);
} else {
const first1 = temp.shift();
if (typeof first1 === "undefined") {
throw Error("Invalid number");
}
nums1.push(first1);
}
}
if (nums2.length) {
nums1.push(...nums2);
}
if (temp.length) {
nums1.push(...temp);
}
}