-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path88.合并两个有序数组_2.js
66 lines (57 loc) · 1.25 KB
/
88.合并两个有序数组_2.js
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*
* @lc app=leetcode.cn id=88 lang=javascript
* @lcpr version=30202
*
* [88] 合并两个有序数组
*/
// @lcpr-template-start
// @lcpr-template-end
// @lc code=start
/**
* @param {number[]} nums1
* @param {number} m
* @param {number[]} nums2
* @param {number} n
* @return {void} Do not return anything, modify nums1 in-place instead.
*/
var merge = function (nums1, m, nums2, n) {
let p1 = m - 1;
let p2 = n - 1;
let last = m + n - 1;
while (p1 >= 0 || p2 >= 0) {
const num1 = nums1[p1];
const num2 = nums2[p2];
if (num2 === undefined) {
nums1[last] = num1;
last -= 1;
p1 -= 1;
} else if (num1 === undefined) {
nums1[last] = num2;
last -= 1;
p2 -= 1;
} else if (num1 >= num2) {
nums1[last] = num1;
last -= 1;
p1 -= 1;
} else {
nums1[last] = num2;
last -= 1;
p2 -= 1;
}
}
};
// @lc code=end
/*
// @lcpr case=start
// [-1,0,0,3,3,3,0,0,0]\n6\n[1,2,2]\n3\n
// @lcpr case=end
// @lcpr case=start
// [1]\n1\n[]\n0\n
// @lcpr case=end
// @lcpr case=start
// [0]\n0\n[1]\n1\n
// @lcpr case=end
*/
// @after-stub-for-debug-begin
module.exports = merge;
// @after-stub-for-debug-end