LeetCode: Merge Sorted Array
Given two sorted integer arrays A and B, merge B into A as one sorted array.
Note:
You may assume that A has enough space (size that is greater or equalM+N) To hold additional elements from B. The number of elements initialized in A and B areMAndNRespectively.
// Https://oj.leetcode.com/problems/merge-sorted-array/// Author: Chao Zeng // Date: 2015-2-2class Solution {public: void merge (int A [], int m, int B [], int n) {int newlength = m + n-1; int length1 = m-1; int lengh2 = n-1; // start to compare the end of the two arrays while (length1> = 0 & length1> = 0) {if (A [length1]> B [length1]) {A [newlength] = A [length1]; length1 --;} else {A [newlength] = B [lengty2]; lengty2 --;} newlength --;} // If array B is longer than array A, while (lengty2> = 0) {A [newlength] = B [lengty2]; lengty2 --; newlength --;}}};