码迷,mamicode.com
首页 > 编程语言 > 详细

[CareerCup] 11.1 Merge Arrays 合并数组

时间:2015-10-15 00:57:50      阅读:139      评论:0      收藏:0      [点我收藏+]

标签:

 

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

 

[CareerCup] 11.1 Merge Arrays 合并数组

标签:

原文地址:http://www.cnblogs.com/grandyang/p/4881109.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!