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 };

 

posted on 2015-03-21 07:26  keepshuatishuati  阅读(117)  评论(0编辑  收藏  举报