- Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathMergeSortedArray.cs
More file actions
Latest commit
executable file
·48 lines (44 loc) · 2.84 KB
/
Copy pathMergeSortedArray.cs
File metadata and controls
executable file
·48 lines (44 loc) · 2.84 KB
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
// Source : https://leetcode.com/problems/merge-sorted-array/?tab=Description
// Author : codeyu
// Date : Saturday, February 18, 2017 10:56:57 PM
/**********************************************************************************
*
* Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.
*
*
* Note:
* You may assume that nums1 has enough space (size that is greater or equal to m + n) to hold additional elements from nums2. The number of elements initialized in nums1 and nums2 are m and n respectively.
*
**********************************************************************************/
usingSystem;
usingSystem.Collections.Generic;
usingAlgorithms.Utils;
namespaceAlgorithms
{
publicclassSolution088
{
publicstaticvoidMerge(int[]nums1,intm,int[]nums2,intn)
{
if(nums1==null||nums2==null)
return;
intidx1=m-1;
intidx2=n-1;
intlen=m+n-1;
while(idx1>=0&&idx2>=0)
{
if(nums1[idx1]>nums2[idx2])
{
nums1[len--]=nums1[idx1--];
}
else
{
nums1[len--]=nums2[idx2--];
}
}
while(idx2>=0)
{
nums1[len--]=nums2[idx2--];
}
}
}
}