LeetCode-Merge Sorted Array-合并有序表-归并排序

https://oj.leetcode.com/problems/merge-sorted-array/

归并排序的一步操作,需要事先把A[]的元素移到数组末端,前段空出来填充结果。需要注意的是如果从0~m的转移会在n比较小的时候有问题。所以要从m~0转移。使用memcpy在GCC下就是从0~m开始转移,这个行为跟MSVC不一样。

class Solution {
public:
    void merge(int A[], int m, int B[], int n) {
        //memcpy(A+n,A,m*sizeof(int));  //error in gcc, succeed in msvc because of filling order
        for(int i=m-1;i>=0;i--) A[i+n]=A[i];
        int p=n;
        int q=0;
        int r=0;
        while(p<n+m && q<n){
            if (A[p]>B[q]) {
                A[r]=B[q];
                r++;q++;
            }
            else {
                A[r]=A[p];
                r++;p++;
            }
        }
        while(p<n+m) A[r++]=A[p++];
        while(q<n) A[r++]=B[q++];
    }
};
时间: 2024-12-29 17:15:02

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 are m

[LeetCode] Merge Sorted Array [22]

题目 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:Merge Sorted Array 归并排序

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

LeetCode: Merge Sorted Array [088]

[题目] 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

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

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

(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 init

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 a

[Leetcode] Merge Sorted Array (C++)

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

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