11.1 You are given two sorted arrays, A and B, where A has a large enough buffer at the end to hold B. Write a method to merge B into A in sorted orde.

 

LeetCode上的原题,请参见我之前的博客Merge Sorted Array 混合插入有序数组

 

class Solution {
public:
    void merge(vector<int> &a, int m, vector<int> &b, int n) {
        int cnt = m + n - 1;
        --m; --n;
        while (m >= 0 && n >= 0) a[cnt--] = a[m] > b[n] ? a[m--] : b[n--];
        while (n >= 0) a[cnt--] = b[n--];
    }
};

 

相关文章:

  • 2021-08-21
  • 2022-12-23
  • 2022-02-19
  • 2021-08-23
  • 2021-06-29
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-11-03
  • 2022-12-23
  • 2021-11-30
  • 2021-07-04
  • 2022-12-23
相关资源
相似解决方案