Skip to content

Instantly share code, notes, and snippets.

@codinfox
Created January 16, 2015 08:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codinfox/eef11ff80cb7fd02200d to your computer and use it in GitHub Desktop.
Save codinfox/eef11ff80cb7fd02200d to your computer and use it in GitHub Desktop.
class Solution {
public:
void merge(int A[], int m, int B[], int n) {
int C[m+n];
int a = 0, b = 0, c = 0;
while (a < m && b < n) {
while (a < m && A[a] <= B[b]) {
C[c++] = A[a++];
}
while (b < n && B[b] <= A[a]) {
C[c++] = B[b++];
}
}
if (a == m) {
while (b < n) C[c++] = B[b++];
} else if (b == n) {
while (a < m) C[c++] = A[a++];
}
for (int cc = 0; cc < m + n; cc++) {
A[cc] = C[cc];
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment