iT邦幫忙

2021 iThome 鐵人賽

DAY 4
1
自我挑戰組

試煉之地 Leetcode 的挑戰系列 第 4

Leetcode 挑戰 Day 04 [88. Merge Sorted Array]

88. Merge Sorted Array


今天要挑戰的合併兩個已排序的陣列,這題的題目要求也很有趣,與以往有些不同,讓我們一起來挑戰看看!

題目


You are given two integer arrays nums1 and nums2, sorted in non-decreasing order, and two integers m and n, representing the number of elements in nums1 and nums2 respectively.

Merge nums1 and nums2 into a single array sorted in non-decreasing order.

The final sorted array should not be returned by the function, but instead be stored inside the array nums1. To accommodate this, nums1 has a length of m + n, where the first m elements denote the elements that should be merged, and the last n elements are set to 0 and should be ignored. nums2 has a length of n.

Example 1:
Input: nums1 = [1,2,3,0,0,0], m = 3, nums2 = [2,5,6], n = 3
Output: [1,2,2,3,5,6]
Explanation: The arrays we are merging are [1,2,3] and [2,5,6].
The result of the merge is [1,2,2,3,5,6] with the underlined elements coming from nums1.

Example 2:
Input: nums1 = [1], m = 1, nums2 = [], n = 0
Output: [1]
Explanation: The arrays we are merging are [1] and [].
The result of the merge is [1].

Example 3:
Input: nums1 = [0], m = 0, nums2 = [1], n = 1
Output: [1]
Explanation: The arrays we are merging are [] and [1].
The result of the merge is [1].
Note that because m = 0, there are no elements in nums1. The 0 is only there to ensure the merge result can fit in nums1.

這個題目敘述真的是有點長,我長話短說簡單說明,這題的要求是,題目會給我們兩個已經排序好的一維陣列nums1與nums2,並希望我們能夠將兩個合併過後,仍然保持排序。

特別的是,這題並不是要求我們回傳一個陣列,而是希望我們能不用額外的空間,直接利用nums1把排序好的陣列存進去!那當然,題目有特別設計過nums1的空間剛好可以放得下合併後的兩個陣列(留空的空間以0表示),而且題目也會給我們兩個參數m與n分別代表nums1與nums2的陣列中數字的個數。

Forloop


這題其實可以用一個小巧思,不需要用任何內建的函式只需要依靠一個迴圈,就能達到將兩個排序好的陣列合併的效果。由於我們知道兩個陣列內分別都是排序好的數字,因此可以分別利用參數m與n,從陣列的最後一個數字開始進行兩兩比對,如果比較大的就將其直接放進nums1的尾巴,如果比較小的數就繼續留著等待比對,最多經過m+n次的迴圈後,就一定能將兩個陣列合併,如果nums2優先排序完,甚至可以直接中斷迴圈,就能達到題目要求。

我們這邊用題目敘述中的example1當例子。首先
Input: nums1 = [1,2,3,0,0,0], m = 3, nums2 = [2,5,6], n = 3
我們比對nums1真正的最後一個數字3與nums2最後一個6,這邊6比較大,所以把6放進nums1的尾巴,會變這樣
nums1 = [1,2,3,0,0,6]
而後下一次輪到nums2中的5和nums1中一樣是3,依序用同樣的邏輯比對,會變這樣
nums1 = [1,2,3,0,5,6]
按照這樣的方法,會依序有下列變化
nums1 = [1,2,3,3,5,6]
nums1 = [1,2,2,3,5,6]
到這邊nums2的東西全部比完,可以中斷迴圈,就是題目要求了。

以下為python3的程式碼

class Solution:
    def merge(self, nums1: List[int], m: int, nums2: List[int], n: int) -> None:
        for i in range(1, m + n + 1):
            if n == 0:
                break
            elif m == 0:
                nums1[-i] = nums2[n - 1]
                n -= 1
                continue
            
            if nums1[m - 1] < nums2[n - 1]:
                nums1[-i] = nums2[n - 1]
                n -= 1
            else:
                nums1[-i] = nums1[m - 1]
                m -= 1

以下是C++的程式碼

class Solution {
public:
    void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {
        for(int i=m+n-1;i>-1;i--){
            if(m==0){
                nums1[i] = nums2[n-1];
                n -= 1;
                continue;
            }
            else if(n==0){
                break;
            }
            if(nums2[n-1] >= nums1[m-1]){
                nums1[i] = nums2[n-1];
                n -= 1;
            }
            else{
                nums1[i] = nums1[m-1];
                m -= 1;
            }
            
        }
    }
};

上一篇
Leetcode 挑戰 Day 03 [20. Valid Parentheses]
下一篇
Leetcode 挑戰 Day 05 [136. Single Number]
系列文
試煉之地 Leetcode 的挑戰19
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言