美文网首页
LeetCode每日一题:归并两个有序数组

LeetCode每日一题:归并两个有序数组

作者: yoshino | 来源:发表于2017-05-16 15:53 被阅读21次

    问题描述

    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 from B. The number of elements initialized in A and B are m and n respectively.

    问题分析

    其实考察的是归并排序,但是更加简便的方法就是两个数组合在一处再排序,虽然能AC,但不符合算法的要求

    代码非正确方法实现

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

    相关文章

      网友评论

          本文标题:LeetCode每日一题:归并两个有序数组

          本文链接:https://www.haomeiwen.com/subject/paplxxtx.html