# [LeetCode]--88. Merge Sorted Array

Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.

Note:
You may assume that nums1 has enough space (size that is greater or equal to m + n) to hold additional elements from nums2. The number of elements initialized in nums1 and nums2 are m and n respectively.

public void merge(int[] nums1, int m, int[] nums2, int n) {
int i = 0, j = 0;
while (i < m && j < n) {
if (nums2[j] < nums1[i]) {
//这里是m，不能是m-1，因为插入一个之后变长了一个
for (int p = m; p > i; p--)
nums1[p] = nums1[p - 1];
nums1[i] = nums2[j];
j++;
m++;
}
i++;
}
while (j < n) {
nums1[i] = nums2[j];
i++;
j++;
}
}

class Solution {
/**
* @param A: sorted integer array A which has m elements,
*           but size of A is m+n
* @param B: sorted integer array B which has n elements
* @return: void
*/
public void mergeSortedArray(int[] A, int m, int[] B, int n) {
int i = m-1, j = n-1, index = m + n - 1;
while (i >= 0 && j >= 0) {
if (A[i] > B[j]) {
A[index--] = A[i--];
} else {
A[index--] = B[j--];
}
}
while (i >= 0) {
A[index--] = A[i--];
}
while (j >= 0) {
A[index--] = B[j--];
}
}
}

#### 【leetcode】88. Merge Sorted Array（Python & C++）

2017-09-05 21:39:01

#### Leetcode_88_Merge Sorted Array

2014-11-30 23:37:26

#### 【leetcode c++】88 Merge Sorted Array

2015-07-27 14:27:46

#### Leetcode：88. Merge Sorted Array（JAVA）

2016-03-13 09:49:08

#### leetCode 88.Merge Sorted Array （合并排序数组） 解题思路和方法

2015-07-25 09:45:45

#### leetcode-java-88. Merge Sorted Array

2016-08-28 10:10:49

#### leetcode 88 Merge Sorted Array

2015-06-24 22:47:51

#### leetcode解题之88 #Merge Sorted Array Java版

2017-03-13 18:57:34

#### LeetCode（88）Merge Sorted Array

2015-08-20 17:33:59

#### 88. Merge Sorted Array [easy] (Python)

2016-06-15 13:54:39