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.

混合插入有序数组,由于两个数组都是有序的,所有只要按顺序比较大小即可。最先想到的方法是建立一个m+n大小的新数组,然后逐个从A和B数组中取出元素比较,把较小的加入新数组,然后在考虑A数组有剩余和B数组有剩余的两种情况,最后在把新数组的元素重新赋值到A数组中即可。代码如下:

class Solution {
public:
    void merge(int A[], int m, int B[], int n) {
        if (m <= 0 && n <= 0) return;
        int a = 0, b = 0;
        int C[m + n];
        for (int i = 0; i < m + n; ++i) {
            if (a < m && b < n) {
                if (A[a] < B[b]) {
                    C[i] = A[a];
                    ++a;
                }
                else {
                    C[i] = B[b];
                    ++b;
                }
            }
            else if (a < m && b >= n) {
                C[i] = A[a];
                ++a;
            }
            else if (a >= m && b < n) {
                C[i] = B[b];
                ++b;
            }
            else return;
        }
        for (int i = 0; i < m + n; ++i) A[i] = C[i];
    }
};

这样固然没错,但是还有更简洁的方法,而且不用申请新变量。算法思想是:由于合并后A数组的大小必定是m+n,所以从最后面开始往前赋值,先比较A和B中最后一个元素的大小,把较大的那个插入到m+n-1的位置上,再依次向前推。如果A中所有的元素都比B小,那么前m个还是A原来的内容,没有改变。如果A中的数组比B大的,当A循环完了,B中还有元素没加入A,直接用个循环把B中所有的元素覆盖到A剩下的位置。代码如下:

class Solution {
public:
    void merge(int A[], int m, int B[], int n) {
        int count = m + n - 1;
        --m; --n;
        while (m >= 0 && n >= 0) A[count--] = A[m] > B[n] ? A[m--] : B[n--];
        while (n >= 0) A[count--] = B[n--];
    }
};
时间: 2024-10-12 00:37:37

Merge Sorted Array 混合插入有序数组的相关文章

LeetCode 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 number of elements initi

【LeetCode】- Merge Sorted Array (合并有序数组)

[ 问题: ] Given two sorted integer arrays A and B, merge B into A as one sorted array. 直译:给定两个排好序的整形数组,将数组B合并到数组A,形成一个新的数组. 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 nu

Merge Two Sorted Lists 混合插入有序链表

Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. 这道混合插入有序链表和我之前那篇混合插入有序数组非常的相似 (http://www.cnblogs.com/grandyang/p/4059650.html),仅仅是数据结构由数组换成了链表而已,代码写起来反而更简洁

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 88 Merge Sorted Array(合并排序数组)(*)

翻译 给定两个排序的整型数组nums1和nums2,将nums2合并到nums1成一个排序数组. 批注: 你可以假设nums1中有足够的空间(空间大于或等于m+n)来存放来自nums2的额外元素. nums1和nums2的初始空间分别是m和n. 原文 Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note: You may assume that nums1

[LeetCode] Search in Rotated Sorted Array 在旋转有序数组中搜索

Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). You are given a target value to search. If found in the array return its index, otherwise return -1. You may assume no duplic

[LeetCode] 80. Remove Duplicates from Sorted Array II ☆☆☆(从有序数组中删除重复项之二)

https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/discuss/27976/3-6-easy-lines-C%2B%2B-Java-Python-Ruby 描述 Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For example,Given sorted array A = [1,1

leetCode 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 number of elements init

[Leetcode] search in rotated sorted array 搜索旋转有序数组

Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e.,0 1 2 4 5 6 7might become4 5 6 7 0 1 2). You are given a target value to search. If found in the array return its index, otherwise return -1. You may assume no duplicate