第十二题 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 nrespectively.

Solution1:新建ArrayList暂存merged list,后放回A

public class Solution {
    public void merge(int A[], int m, int B[], int n) {
        int length = m+n;
        ArrayList<Integer> merge = new ArrayList<Integer>();
        int posA =0, posB=0, i=0;
        while( i<length){
            if(posB>=n){merge.add(A[posA]); posA++; i++;}
            else if(posA>=m){ merge.add(B[posB]); posB++; i++;}
            else if(A[posA]<=B[posB]){merge.add(A[posA]); posA++; i++;}
            else{merge.add(B[posB]); posB++; i++;}
        }
        for(i=0; i<length;i++){
            A[i] = merge.get(i);
        }
    }
}

Solution2:可不能够不额外开辟空间?利用倒序。

public class Solution {
    public void merge(int A[], int m, int B[], int n) {
        int position = m+n-1;
        while(m>0 && n>0){
            if(A[m-1]>=B[n-1]) {
                A[position] = A[m-1];
                m--;

            }
            else{
                A[position] = B[n-1];
                n--; 

            }
            position--;
        }
        while(n>0){
            A[position] = B[n-1];
            n--;
            position--;
        }
    }
}

Solution1:再简单点?

public class Solution {
    public void merge(int A[], int m, int B[], int n) {

        for(int i=m+n-1; i>=0; i--) A[i]=((m>0)&&(n==0 || A[m-1]>=B[n-1]))?A[--m]:B[--n];

    }
}
时间: 2024-10-12 13:59:25

第十二题 Merge Sorted Array的相关文章

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

leetcode第88题-Merge Sorted Array

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

[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

算法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

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

88. Merge Sorted Array【leetcode】算法,java将两个有序数组合并到一个数组中

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 n

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

[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