Note:
Since it merged from end, so
1. tmp1, tmp2 = INT_MIN
2. tmp1 > tmp2
1 class Solution { 2 public: 3 void merge(int A[], int m, int B[], int n) { 4 int total = m+n-1, indexA = m-1, indexB = n-1; 5 while (indexA >= 0 || indexB >= 0) { 6 int tmp1 = INT_MIN, tmp2 = INT_MIN; 7 if (indexA >= 0) { 8 tmp1 = A[indexA]; 9 } 10 if (indexB >= 0) { 11 tmp2 = B[indexB]; 12 } 13 if (tmp1 > tmp2) { 14 A[total--] = A[indexA--]; 15 } else { 16 A[total--] = B[indexB--]; 17 } 18 } 19 } 20 };
时间: 2024-10-16 14:22:29