[LintCode] 合并排序数组II
1 class Solution { 2 public: 3 /** 4 * @param A: sorted integer array A which has m elements, 5 * but size of A is m+n 6 * @param B: sorted integer array B which has n elements 7 * @return: void 8 */ 9 void mergeSortedArray(int A[], int m, int B[], int n) { 10 // write your code here 11 int pa = m - 1, pb = n - 1, p = m + n - 1; 12 while (pa >= 0 && pb >= 0) { 13 if (A[pa] >= B[pb]) A[p--] = A[pa--]; 14 else A[p--] = B[pb--]; 15 } 16 while (pb >= 0) A[p--] = B[pb--]; 17 } 18 };
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步