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 equal to m + n) to hold additional elements from B. The number of elements initialized in A and B are m andn respectively.

// https://oj.leetcode.com/problems/merge-sorted-array/
// Author : Chao Zeng
// Date   : 2015-2-2

class Solution {
public:
    void merge(int A[], int m, int B[], int n) {
        int newlength = m + n - 1;
        int length1 = m - 1;
        int length2 = n - 1;
        //两个数组末尾开始比较
        while(length1 >= 0 && length2 >= 0){
            if (A[length1] > B[length2]){
                A[newlength] = A[length1];
                length1--;
            }
            else{
                A[newlength] = B[length2];
                length2--;
            }
            newlength--;
        }
        //如果数组B比数组A长
        while(length2 >= 0){
            A[newlength] = B[length2];
            length2--;
            newlength--;
        }
    }
};
时间: 2024-10-13 11:47:23

LeetCode:Merge Sorted Array的相关文章

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 equal to m + n) to hold additional elements from B. The number of elements initialized in A and B ar

LeetCode --- 88. Merge Sorted Array

题目链接: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 equal to m + n) to hold additional elements from B. The number of elements init

LeetCode OJ - Merge Sorted Array

原地归并. 下面是AC代码: 1 public void merge(int A[], int m, int B[], int n) { 2 3 int len = A.length; 4 //first copy m elements of A to the end of A 5 for(int i=m-1,j = len-1;i>=0;i--){ 6 A[j--] = A[i]; 7 } 8 //merge the A and B 9 int startA = len - m; 10 int

leetCode 88. Merge Sorted Array 有序数组

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 n

<LeetCode OJ> Merge Sorted Array【88】

88. Merge Sorted Array My Submissions Question Total Accepted: 80830 Total Submissions: 274120 Difficulty: Easy Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note: You may assume that nums1 has enough sp

LeetCode (1) 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 to hold additional elements from B. The number of elements initialized in A and B are m and n respectively. 刚开始根据归并排序写的多了一个中

【leetcode】Merge Sorted Array

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 equal to m + n) to hold additional elements from B. The number of elements initialize

Leetcode题目: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 i

LeetCode OJ: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 initi