Skip to content

Commit

Permalink
Create Merge Sorted Array.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
liuyl committed Feb 6, 2013
1 parent e6bebd8 commit 3743e5c
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions Merge Sorted Array.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class Solution {
public:
void merge(int A[], int m, int B[], int n) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int *arr = new int[m+n];
memset(arr, 0, (m+n)*sizeof(int));
int ind = 0, ind1 = 0, ind2 = 0;

while(true){
if (ind1 == m && ind2 == n)
break;

if (ind2==n || (ind1 < m && A[ind1] < B[ind2]))
arr[ind++] = A[ind1++];
else
arr[ind++] = B[ind2++];
}

memcpy(A, arr, (m+n)*sizeof(int));

return ;
}
};

0 comments on commit 3743e5c

Please sign in to comment.