[CareerCup] 11.1 Merge Arrays 合并数组

简介:

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

 本文转自博客园Grandyang的博客,原文链接:合并数组[CareerCup] 11.1 Merge Arrays ,如需转载请自行联系原博主。

相关文章
|
算法 测试技术
LeetCode 88. 合并两个有序数组 Merge Sorted Array
LeetCode 88. 合并两个有序数组 Merge Sorted Array
LeetCode 21. 合并两个有序链表 Merge Two Sorted Lists
LeetCode 21. 合并两个有序链表 Merge Two Sorted Lists
LeetCode 88. Merge Sorted Array
题意是给定了两个排好序的数组,让把这两个数组合并,不要使用额外的空间,把第二个数组放到第一个数组之中.
65 0
LeetCode 88. Merge Sorted Array
|
Java Python
LeetCode 21:合并两个有序链表 Merge Two Sorted Lists
将两个有序链表合并为一个新的有序链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。 Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. 示例: 输入:1->2->4, 1->3->4 输出:1->1->2->3->4->4 解题思路: 迭代和递归都能解题。
963 0
|
Java
[LeetCode]Merge Sorted Array 合并排序数组
链接:https://leetcode.com/problems/merge-sorted-array/description/难度:Easy题目:88.
984 0