Created
January 16, 2015 08:58
-
-
Save codinfox/eef11ff80cb7fd02200d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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