forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.js
39 lines (38 loc) · 809 Bytes
/
Solution.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
//beat 88%
const merge1 = function(nums1, m, nums2, n){
const arr = nums1.slice(0,m);
let i = 0, j = 0;
let count = 0;
while(i < m && j < n){
if(arr[i] <= nums2[j]){
nums1[count++] = arr[i++];
}else{
nums1[count++] = nums2[j++];
}
}
while(i < m){
nums1[count++] = arr[i++];
}
while(j < n){
nums1[count++] = nums2[j++];
}
};
//beat 30%....
const merge = function(nums1, m, nums2,n){
let index = m + n - 1;
let aindex = m - 1;
let bindex = n - 1;
while(aindex >= 0 && bindex >= 0){
if(nums1[aindex] > nums2[bindex]){
nums1[index--] = nums1[aindex--];
}else{
nums1[index--] = nums2[bindex--];
}
}
while(aindex >= 0){
nums1[index--] = nums1[aindex--];
}
while(bindex >= 0){
nums1[index--] = nums2[bindex--];
}
};