Leetcode-Merge Sorted Array


Given two sorted integer arrays A and B, merge B into A as one sorted array.


You may assume that A has enough space to hold additional elements from B. The number of elements initialized in A and B are m and n respectively.


class Solution {
	void merge(int A[], int m, int B[], int n) {
	// IMPORTANT: Please reset any member data you declared, as
	// the same Solution instance will be reused for each test case.
	int cur = m - 1;
		for(int i = 0; i < n; i++)
			while(cur >= 0 && A[cur] >= B[n-1-i])
				A[cur+n-i] = A[cur--];
			A[cur+n-i] = B[n-1-i];