Skip to content

Commit e9c5ae2

Browse files
committed
https://leetcode-cn.com/problems/merge-sorted-array/
1 parent 07af2fb commit e9c5ae2

File tree

6 files changed

+101
-5
lines changed

6 files changed

+101
-5
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ https://leetcode-cn.com/problems/climbing-stairs/
2222

2323
https://leetcode-cn.com/problems/fibonacci-number/
2424

25+
https://leetcode-cn.com/problems/merge-sorted-array/
26+
2527
#### 安装教程
2628

2729
1. 安装`deno`

climbing-stairs/test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ Deno.test("climbing-stairs", () => {
1010
{ input: 1, output: 1 },
1111
{ input: 2, output: 2 },
1212
{ input: 3, output: 3 },
13+
{ input: 45, output: 1836311903 },
14+
{ input: 44, output: 1134903170 },
15+
{ input: 46, output: 1134903170 + 1836311903 },
1316
{ input: 99, output: 354224848179261915075n },
1417
];
1518
examples.forEach(({ input, output }) => {

deps.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ import {
33
assertEquals,
44
assertStrictEquals,
55
equal,
6-
} from "https://deno.land/std@0.133.0/testing/asserts.ts";
6+
} from "https://deno.land/std@0.134.0/testing/asserts.ts";
77
export { assert, assertEquals, assertStrictEquals, equal };

merge-sorted-array/index.ts

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/**
2+
Do not return anything, modify nums1 in-place instead.
3+
*/
4+
export default function merge(
5+
nums1: number[],
6+
m: number,
7+
nums2: number[],
8+
n: number,
9+
): void {
10+
if (n === 0) {
11+
return;
12+
}
13+
if (m == 0) {
14+
nums1.splice(0, n, ...nums2);
15+
return;
16+
}
17+
const temp = nums1.slice(0, m);
18+
nums1.length = 0;
19+
while (temp.length > 0 && nums2.length > 0) {
20+
if (temp[0] > nums2[0]) {
21+
const first2 = nums2.shift();
22+
if (typeof first2 === "undefined") {
23+
throw Error("Invalid number");
24+
}
25+
nums1.push(first2);
26+
} else {
27+
const first1 = temp.shift();
28+
if (typeof first1 === "undefined") {
29+
throw Error("Invalid number");
30+
}
31+
nums1.push(first1);
32+
}
33+
}
34+
if (nums2.length) {
35+
nums1.push(...nums2);
36+
}
37+
if (temp.length) {
38+
nums1.push(...temp);
39+
}
40+
}
41+
// /**
42+
// Do not return anything, modify nums1 in-place instead.
43+
// */
44+
// function merge(nums1: number[], m: number, nums2: number[], n: number): void {
45+
// nums1.splice(m, nums1.length - m, ...nums2);
46+
// nums1.sort((a, b) => a - b);
47+
// // let p1 = 0, p2 = 0;
48+
// // const sorted = new Array(m + n).fill(0);
49+
// // var cur;
50+
// // while (p1 < m || p2 < n) {
51+
// // if (p1 === m) {
52+
// // cur = nums2[p2++];
53+
// // } else if (p2 === n) {
54+
// // cur = nums1[p1++];
55+
// // } else if (nums1[p1] < nums2[p2]) {
56+
// // cur = nums1[p1++];
57+
// // } else {
58+
// // cur = nums2[p2++];
59+
// // }
60+
// // sorted[p1 + p2 - 1] = cur;
61+
// // }
62+
// // for (let i = 0; i != m + n; ++i) {
63+
// // nums1[i] = sorted[i];
64+
// // }
65+
66+
// };

merge-sorted-array/test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { assertEquals } from "../deps.ts";
2+
import { mergeSortedArray } from "../mod.ts";
3+
Deno.test("merge-sorted-array", () => {
4+
const examples: {
5+
input: Parameters<typeof mergeSortedArray>;
6+
output: Parameters<typeof mergeSortedArray>[0];
7+
}[] = [
8+
{
9+
input: [[1, 2, 3, 0, 0, 0], 3, [2, 5, 6], 3],
10+
output: [1, 2, 2, 3, 5, 6],
11+
},
12+
{ input: [[1], 1, [], 0], output: [1] },
13+
{ input: [[0], 0, [1], 1], output: [1] },
14+
];
15+
examples.forEach(({ input, output }) => {
16+
mergeSortedArray(...input);
17+
assertEquals(output, input[0]);
18+
});
19+
});

mod.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,23 @@
1+
import mergeSortedArray from "./merge-sorted-array/index.ts";
2+
13
import htmlEntityParser from "./html-entity-parser/index.ts";
2-
export { htmlEntityParser };
4+
35
export {
46
ArrayToListNode,
57
ListNode,
68
ListNodeToArray,
79
} from "./reverse-linked-list/index.ts";
810
import reverseLinkedList from "./reverse-linked-list/index.ts";
9-
export { reverseLinkedList };
11+
1012
import climbStairs from "./climbing-stairs/index.ts";
1113
import fei_bo_na_qi_shu_lie_lcof from "./fei-bo-na-qi-shu-lie-lcof/index.ts";
12-
export { climbStairs };
13-
export { fei_bo_na_qi_shu_lie_lcof };
14+
1415
import fibonacciNumber from "./fibonacci-number/index.ts";
1516
import twoSum from "./two-sum/index.ts";
1617
export { fibonacciNumber };
1718
export { twoSum };
19+
export { mergeSortedArray };
20+
export { htmlEntityParser };
21+
export { reverseLinkedList };
22+
export { climbStairs };
23+
export { fei_bo_na_qi_shu_lie_lcof };

0 commit comments

Comments
 (0)