# ＃6 Merge Two Sorted Arrays

Merge two given sorted integer array A and B into a new sorted integer array.

Example

A=[1,2,3,4]

B=[2,4,5,6]

return [1,2,2,3,4,4,5,6]

two pointers的简单题，就不多说了。

Mycode（AC ＝ 13ms）：

class Solution {
public:
/**
* @param A and B: sorted integer array A and B.
* @return: A new sorted integer array
*/
vector<int> mergeSortedArray(vector<int> &A, vector<int> &B) {
vector<int> ans(A.size() + B.size(), 0);

if (A.size() == 0) return ans;

int idx1 = 0, idx2 = 0, idx = 0;

while (idx1 < A.size() && idx2 < B.size()) {
if (A[idx1] <= B[idx2]) {
ans[idx++] = A[idx1++];
}
else {
ans[idx++] = B[idx2++];
}
}

while (idx1 < A.size()) {
ans[idx++] = A[idx1++];
}

while (idx2 < B.size()) {
ans[idx++] = B[idx2++];
}

return ans;
}
};

#### Lintcode6 Merge Two Sorted Arrays solution 题解

2017-04-01 18:51:25

#### LintCode Merge Sorted Array 合并排序数组

2015-07-14 16:00:41

#### Merge k Sorted Arrays

2016-05-30 21:45:07

#### LintCode 6. Merge Two Sorted Arrays

2018-02-22 15:08:31

#### 【leetcode c++】21 Merge Two Sorted Lists

2015-06-18 13:37:28

#### Leetcode:21. Merge Two Sorted Lists(JAVA)

2016-03-03 18:59:00

#### C实现 LeetCode->Merge Two Sorted Lists （双指针大法）

2015-06-18 12:02:05

#### leetcode之 median of two sorted arrays

2013-09-10 00:15:50

#### leetcode04-Median of Two Sorted Arrays-python

2016-03-31 15:11:13

#### 《LeetBook》leetcode题解(4): Median of Two Sorted Arrays[H]——两个有序数组中值问题

2016-04-09 21:24:06