leetcode || 88、Merge Sorted Array

problem:

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 mand n respectively.

Hide Tags

Array Two
Pointers

题意:原址合并两个已序数组

thinking:

(1)先合并再排序,O(m+n)lo(m+n)

(2)从后往前比较插入,不用移动元素,O(m+n)

code:

合并排序

class Solution {
public:
    void merge(int A[], int m, int B[], int n) {
        for(int i=0;i<n;i++)
            A[m+i]=B[i];
        sort(A,A+m+n);
    }
};

从后往前比较插入

class Solution {
public:
    void merge(int A[], int m, int B[], int n) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        int index = m + n - 1;
        int aIndex = m - 1;
        int bIndex = n - 1;
        while(0 <= aIndex && 0 <= bIndex)
        {
            if (B[bIndex] > A[aIndex])
            {
                A[index--] = B[bIndex--];
            }
            else
            {
                A[index--] = A[aIndex--];
            }
        }

        while(0 <= aIndex)
        {
            A[index--] = A[aIndex--];
        }

        while(0 <= bIndex)
        {
            A[index--] = B[bIndex--];
        }
    }
};
时间: 2024-10-23 23:09:03

leetcode || 88、Merge Sorted Array的相关文章

LeetCode(88)题解-- Merge Sorted Array

https://leetcode.com/problems/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 additi

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

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

[LC]88题 Merge Sorted Array (合并两个有序数组 )

①英文题目 Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note: The number of elements initialized in nums1 and nums2 are m and n respectively.You may assume that nums1 has enough space (size that is greater o

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

leetcode第88题-Merge Sorted Array

题目的意思:合并两个有序的数组到第一个数组中,使合并之后的数组仍然有序,且假设第一个数组有足够的空间. 解题思路:一开始遇到这个题目,我也是不顾一切的从头开始遍历,结果很麻烦,总是有一两个位置走不对,数组并不像链表那样插入一个节点这么简单,我们要解决的就是插入节点的位置问题.所幸,上网查了一下其他人的做法,突然想到了可以从后往前遍历,因为每个数组都是有序的,比较两个数组的最后一个位置的元素大小就可以确定该元素的最终位置,最后就利用这个想法AC了这道题. #include<stdio.h> #i

LeetCode算法题-Merge Sorted Array(Java实现)

这是悦乐书的第161次更新,第163篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第20题(顺位题号是88).给定两个排序的整数数组nums1和nums2,将nums2中的元素合并到nums1中,并且作为一个排序的数组.在nums1和nums2中初始化的元素个数分别为m和n.假设nums1有足够的空间(大于或等于m + n)来保存nums2中的其他元素.例如: 输入:nums1 = [1,2,3,0,0,0],m = 3,nums2 = [2,5,6],n = 3