本文共 885 字,大约阅读时间需要 2 分钟。
class Solution {//write down some cases and find out the regular patternpublic: void merge(int A[], int m, int B[], int n) { // Start typing your C/C++ solution below // DO NOT write int main() function int i = m-1; int j = n-1; int now = m+n-1; while(i >= 0 && i < m && j >= 0 && j < n) { if(A[i] > B[j]) A[now--] = A[i--]; else A[now--] = B[j--]; } while (j >= 0 && j < n) A[now--] = B[j--]; }};
second time
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 end = m+n-1; int i = m-1; int j = n-1; while(i >= 0 && j >= 0 && end >= 0) { if(A[i] >= B[j]) A[end--] = A[i--]; else A[end--] = B[j--]; } while(i >= 0) A[end--] = A[i--]; while(j >= 0) A[end--] = B[j--]; }};
转载地址:http://hlxti.baihongyu.com/