LeetCode88——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 and n respectively.

void merge(int A[], int m, int B[], int n) {}

题目大意

给定两个排好序的int数组A和B,把B合并到A,合并后依然有序。假设A有足够的空间。

难度系数

容易

实现

void merge(int A[], int m, int B[], int n) {
    int temp[m];
    memcpy(temp, A, sizeof(int)*m);
    bool desc = A[0] > A[m-1];
    for (int k = 0, i = 0, j = 0; k < m + n; ++k) {
        if (i >= m) {
            A[k] = B[j++];
        } else if (j >= n) {
            A[k] = temp[i++];
        } else if (desc) {
            A[k] = temp[i] >= B[j] ? temp[i++] : B[j++];
        } else {
            A[k] = temp[i] <= B[j] ? temp[i++] : B[j++];
        }
    }
}

这次让我欣喜的是,一次提交就通过了。不过我发现,题目似乎不需要考虑升序还是降序的问题。 我怎么也没读出题目有默认升序的题意,但是网上都按照升序来做的,这一点我感到很纳闷,当然作为面试题,假定是升序也是可以的, 毕竟降序的写法类似,是升序还是降序,这没啥好考察的。反正我的实现不管升降都没问题吧。

时间: 2024-10-15 10:43:01

LeetCode88——Merge Sorted Array的相关文章

LeetCode88 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 --- 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] 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

[lintcode easy]Merge Sorted Array II

Merge Sorted Array II Merge two given sorted integer array A and B into a new sorted integer array. Example A=[1,2,3,4] B=[2,4,5,6] return [1,2,2,3,4,4,5,6] Challenge How can you optimize your algorithm if one array is very large and the other is ver

[lintcode easy]Merge Sorted Array

Merge Sorted Array Given two sorted integer arrays A and B, merge B into A as one sorted array. Have you met this question in a real interview? Yes Which company asked you this question? Airbnb Alibaba Amazon Apple Baidu Bloomberg Cisco Dropbox Ebay

【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

43. Merge Sorted Array &amp;&amp; LRU Cache

Merge Sorted Array OJ: https://oj.leetcode.com/problems/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 add

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

算法5--排序--Merge Sorted Array

之前几天在忙其他的事情,没有时间更新,今天更新了几个,虽然有几个SMR的博客暂时没有开放,已经写好了,以后会慢慢开放的 今天再更新一个有关排序的算法题 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 fr