1. <strong id="7actg"></strong>
    2. <table id="7actg"></table>

    3. <address id="7actg"></address>
      <address id="7actg"></address>
      1. <object id="7actg"><tt id="7actg"></tt></object>

        ?LeetCode刷題實戰(zhàn)88:合并兩個有序數(shù)組

        共 2183字,需瀏覽 5分鐘

         ·

        2020-11-07 02:16

        算法的重要性,我就不多說了吧,想去大廠,就必須要經(jīng)過基礎(chǔ)知識和業(yè)務(wù)邏輯面試+算法面試。所以,為了提高大家的算法能力,這個公眾號后續(xù)每天帶大家做一道算法題,題目就從LeetCode上面選 !

        今天和大家聊的問題叫做?合并兩個有序數(shù)組,我們先來看題面:

        https://leetcode-cn.com/problems/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 equal to m + n) to hold additional elements from nums2.

        題意


        給你兩個有序整數(shù)數(shù)組 nums1 和 nums2,請你將 nums2 合并到 nums1 中,使 nums1 成為一個有序數(shù)組。

        說明:初始化 nums1 和 nums2 的元素數(shù)量分別為 m 和 n 。
        你可以假設(shè) nums1 有足夠的空間(空間大小大于或等于 m + n)來保存 nums2 中的元素。

        樣例

        輸入:

        nums1 = [1,2,3,0,0,0], m = 3
        nums2 = [2,5,6], n = 3

        輸出:[1,2,2,3,5,6]


        解題


        方法一 : 合并后排序

        最樸素的解法就是將兩個數(shù)組合并之后再排序。,時間復(fù)雜度較差,為O((n+m)log(n+m))。這是由于這種方法沒有利用兩個數(shù)組本身已經(jīng)有序這一點。

        class?Solution?{
        ??public?void?merge(int[] nums1, int?m, int[] nums2, int?n)?{
        ????System.arraycopy(nums2, 0, nums1, m, n);
        ????Arrays.sort(nums1);
        ??}
        }


        方法二 : 雙指針 / 從前往后

        一般而言,對于有序數(shù)組可以通過 雙指針法 達到O(n + m)的時間復(fù)雜度。
        最直接的算法實現(xiàn)是將指針p1 置為 nums1的開頭, p2為 nums2的開頭,在每一步將最小值放入輸出數(shù)組中。
        由于 nums1 是用于輸出的數(shù)組,需要將nums1中的前m個元素放在其他地方,也就需要 O(m)的空間復(fù)雜度。


        class?Solution?{
        ??public?void?merge(int[] nums1, int?m, int[] nums2, int?n)?{
        ????// Make a copy of nums1.
        ????int?[] nums1_copy = new?int[m];
        ????System.arraycopy(nums1, 0, nums1_copy, 0, m);

        ????// Two get pointers for nums1_copy and nums2.
        ????int?p1 = 0;
        ????int?p2 = 0;

        ????// Set pointer for nums1
        ????int?p = 0;

        ????// Compare elements from nums1_copy and nums2
        ????// and add the smallest one into nums1.
        ????while?((p1 < m) && (p2 < n))
        ??????nums1[p++] = (nums1_copy[p1] < nums2[p2]) ? nums1_copy[p1++] : nums2[p2++];

        ????// if there are still elements to add
        ????if?(p1 < m)
        ??????System.arraycopy(nums1_copy, p1, nums1, p1 + p2, m + n - p1 - p2);
        ????if?(p2 < n)
        ??????System.arraycopy(nums2, p2, nums1, p1 + p2, m + n - p1 - p2);
        ??}
        }


        好了,今天的文章就到這里,如果覺得有所收獲,請順手點個在看或者轉(zhuǎn)發(fā)吧,你們的支持是我最大的動力。


        上期推文:

        LeetCode50-80題匯總,速度收藏!
        LeetCode刷題實戰(zhàn)81:搜索旋轉(zhuǎn)排序數(shù)組 II
        LeetCode刷題實戰(zhàn)82:刪除排序鏈表中的重復(fù)元素 II
        LeetCode刷題實戰(zhàn)83:刪除排序鏈表中的重復(fù)元素
        LeetCode刷題實戰(zhàn)84:?柱狀圖中最大的矩形
        LeetCode刷題實戰(zhàn)85:最大矩形
        LeetCode刷題實戰(zhàn)86:分隔鏈表
        LeetCode刷題實戰(zhàn)87:擾亂字符串

        瀏覽 26
        點贊
        評論
        收藏
        分享

        手機掃一掃分享

        分享
        舉報
        評論
        圖片
        表情
        推薦
        點贊
        評論
        收藏
        分享

        手機掃一掃分享

        分享
        舉報
        1. <strong id="7actg"></strong>
        2. <table id="7actg"></table>

        3. <address id="7actg"></address>
          <address id="7actg"></address>
          1. <object id="7actg"><tt id="7actg"></tt></object>
            粉嫩小穴在线观看 | 老子影院午夜伦不卡大全 | 欧美视频在线播放 | 五月丁香激情视频 | 精品久草 | 啊高潮了嗯~喷水了啊 | 美女视频黄a美女视频大全免费看 | 中文字幕亚洲无码视频 | 不卡片| 亚洲乱码国产乱码精品精软件 |