FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
Leetcode/algorithms/MergeTwoSortedArray.js at master · superman06/Leetcode · GitHub
superman06
/
Leetcode
Public
forked from
dnshi/Leetcode
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
Leetcode
/
algorithms
/
MergeTwoSortedArray.js
Copy path
More file actions
More file actions
Latest commit
History
History
History
39 lines (34 loc) · 1.12 KB
Breadcrumbs
Leetcode
/
algorithms
/
MergeTwoSortedArray.js
Copy path
File metadata and controls
39 lines (34 loc) · 1.12 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
// Source : https://oj.leetcode.com/problems/merge-sorted-array/
// Author : Dean Shi
// Date : 2015-05-29
/**********************************************************************************
*
* Given two sorted integer arrays A and B, merge B into A as one sorted array.
*
* Note:
* You may assume that A has enough space (size that is greater or equal to m + n)
* to hold additional elements from B. The number of elements initialized in A and B
* are m and n respectively.
*
**********************************************************************************/
/**
*
@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
i
=
m
-
1
,
j
=
n
-
1
,
k
=
m
+
n
-
1
while
(
i
>=
0
&&
j
>=
0
)
{
nums1
[
k
--
]
=
nums1
[
i
]
>
nums2
[
j
]
?
nums1
[
i
--
]
:
nums2
[
j
--
]
}
while
(
j
>=
0
)
{
nums1
[
k
--
]
=
nums2
[
j
--
]
}
}
;
// Test case
var
m
=
[
4
,
5
,
6
,
0
,
0
,
0
]
;
var
n
=
[
1
,
2
,
3
]
;
merge
(
m
,
3
,
n
,
3
)
;
console
.
log
(
m
.
toString
(
)
===
'1,2,3,4,5,6'
)
;
// true
Back
|
FazBrowse Home
|
New Git URL