Skip to content

Commit bb30904

Browse files
add js solution for 088
1 parent d989e12 commit bb30904

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//beat 88%
2+
const merge1 = function(nums1, m, nums2, n){
3+
const arr = nums1.slice(0,m);
4+
let i = 0, j = 0;
5+
let count = 0;
6+
while(i < m && j < n){
7+
if(arr[i] <= nums2[j]){
8+
nums1[count++] = arr[i++];
9+
}else{
10+
nums1[count++] = nums2[j++];
11+
}
12+
}
13+
while(i < m){
14+
nums1[count++] = arr[i++];
15+
}
16+
while(j < n){
17+
nums1[count++] = nums2[j++];
18+
}
19+
};
20+
21+
//beat 30%....
22+
const merge = function(nums1, m, nums2,n){
23+
let index = m + n - 1;
24+
let aindex = m - 1;
25+
let bindex = n - 1;
26+
while(aindex >= 0 && bindex >= 0){
27+
if(nums1[aindex] > nums2[bindex]){
28+
nums1[index--] = nums1[aindex--];
29+
}else{
30+
nums1[index--] = nums2[bindex--];
31+
}
32+
}
33+
while(aindex >= 0){
34+
nums1[index--] = nums1[aindex--];
35+
}
36+
while(bindex >= 0){
37+
nums1[index--] = nums2[bindex--];
38+
}
39+
};

0 commit comments

Comments
 (0)