File tree Expand file tree Collapse file tree 1 file changed +39
-0
lines changed
solution/088.Merge Sorted Array Expand file tree Collapse file tree 1 file changed +39
-0
lines changed Original file line number Diff line number Diff line change
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
+ } ;
You can’t perform that action at this time.
0 commit comments