slot往前跑不可能跑的比ptA快,如果slot跑进A的原始领地,如果侵占了n个,那表示也用了n个A,所以A的指针永远在slot前
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| class Solution { public: void merge(vector<int>& A, int m, vector<int>& B, int n) { int slot = m + n - 1; int ptA = m - 1; int ptB = n - 1; while(ptA >= 0 && ptB >= 0) { if(A[ptA] > B[ptB]) A[slot--] = A[ptA--]; else A[slot--] = B[ptB--]; } while(slot >= 0) { if(ptA >= 0) A[slot--] = A[ptA--]; else A[slot--] = B[ptB--]; } } };
|