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 are m andn respectively.

【题意】

给定两个有序的数组A和B,把两个数组合并成一个数组。

注意,本题要求把B合并到A中去,并保证A的空间足够大。

【思路】

如果按照以往处理链表合并或者申请新空间来合并的一贯做法,即从头开始依次合并,在本题的环境下会造成A中元素的大量移动。

我们注意到A中有足够的空间,A数组后面的空间可以利用起来。我们换个方向,从数组尾部向头部合并,这样就避免了A中元素的移动。

【代码】

class Solution {
public:
    void merge(int A[], int m, int B[], int n) {
        int pa=m-1;
        int pb=n-1;
        int p=n+m-1;
        while(pa>=0 && pb>=0){
            if(A[pa]>B[pb]){
                A[p--]=A[pa--];
            }
            else{
                A[p--]=B[pb--];
            }
        }
        while(pb>=0){
            A[p--]=B[pb--];
        }
    }
};

LeetCode: Merge Sorted Array [088],布布扣,bubuko.com

时间: 2024-10-12 08:35:03

LeetCode: Merge Sorted Array [088]的相关文章

[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

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 归并排序

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

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

(每日算法)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 are m

Array——LeetCode——Merge Sorted Array

[学到的知识点--当有两个判断时,谁在外面谁在里面]1.两个判断 判断一:a和b是否会超出m和n 判断二:nums1[a]大,还是nums2[b]大2.很显然判断一的优先级大于判断二-----------------------------------------------------------------------------------------------------[反思]1.审题,题目说了nums1有足够的空间----------------------------------