-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMerge_Sorted_Array.cpp
More file actions
69 lines (57 loc) · 1.32 KB
/
Merge_Sorted_Array.cpp
File metadata and controls
69 lines (57 loc) · 1.32 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Source : https://oj.leetcode.com/problems/merge-sorted-array/
// Author : zheng yi xiong
// Date : 2015-01-06
/**********************************************************************************
*
* 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.
*
**********************************************************************************/
#include "stdafx.h"
#include <iostream>
using namespace std;
class Solution {
public:
void merge(int A[], int m, int B[], int n) {
if (0 == n)
{
return;
}
if (0 == m)
{
memcpy(A, B, sizeof(int) * n);
return;
}
memmove(&A[n], &A[0], sizeof(int) * m);
int unit = 0, a = n, b = 0;
for (; (unit < m + n) && (a < m + n) && (b < n); ++unit)
{
if (A[a] < B[b])
{
A[unit] = A[a++];
}
else
{
A[unit] = B[b++];
}
}
if ( (unit < m + n) && (b < n) )
{
while (b < n)
{
A[unit++] = B[b++];
}
}
}
};
int _tmain(int argc, _TCHAR* argv[])
{
int m = 9;
int n = 6;
int A[15] = {1, 4, 5, 7, 9, 13, 23, 45, 67};
int B[] = {2, 6, 8, 33, 56, 87};
Solution so;
so.merge(A, m, B, n);
return 0;
}