• [LeetCode] 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.

    Constraints:

    • nums1.length == m + n
    • nums2.length == n
    • 0 <= m, n <= 200
    • 1 <= m + n <= 200
    • -109 <= nums1[i], nums2[j] <= 109

    Follow up: Can you come up with an algorithm that runs in O(m + n) time?

    合并两个有序数组。

    给你两个按 非递减顺序 排列的整数数组 nums1 和 nums2,另有两个整数 m 和 n ,分别表示 nums1 和 nums2 中的元素数目。

    请你 合并 nums2 到 nums1 中,使合并后的数组同样按 非递减顺序 排列。

    注意:最终,合并后数组不应由函数返回,而是存储在数组 nums1 中。为了应对这种情况,nums1 的初始长度为 m + n,其中前 m 个元素表示应合并的元素,后 n 个元素为 0 ,应忽略。nums2 的长度为 n 。

    来源:力扣(LeetCode)
    链接:https://leetcode-cn.com/problems/merge-sorted-array
    著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

    既然两个数组都已经排好序,而且nums1的size保证能塞得下两个数组长度之和。思路是从后往前比较谁的元素大,谁就把元素放到nums1里面。这样nums1会被下标从右到左,值从大到小被填满。

    时间O(m + n)

    空间O(1)

    Java实现

     1 class Solution {
     2     public void merge(int[] nums1, int m, int[] nums2, int n) {
     3         m = m - 1;
     4         n = n - 1;
     5         int i = m + n + 1;
     6         while (m >= 0 || n >= 0) {
     7             if (m < 0) {
     8                 nums1[i] = nums2[n];
     9                 i--;
    10                 n--;
    11             } else if (n < 0) {
    12                 nums1[i] = nums1[m];
    13                 i--;
    14                 m--;
    15             } else {
    16                 nums1[i--] = nums1[m] > nums2[n] ? nums1[m--] : nums2[n--];
    17             }
    18         }
    19     }
    20 }

    JavaScript实现

     1 /**
     2  * @param {number[]} nums1
     3  * @param {number} m
     4  * @param {number[]} nums2
     5  * @param {number} n
     6  * @return {void} Do not return anything, modify nums1 in-place instead.
     7  */
     8 var merge = function(nums1, m, nums2, n) {
     9     m = m - 1;
    10     n = n - 1;
    11     let i = m + n + 1;
    12     while (m >= 0 || n >= 0) {
    13         if (m < 0) {
    14             nums1[i] = nums2[n];
    15             i--;
    16             n--;
    17         } else if (n < 0) {
    18             nums1[i] = nums1[m];
    19             i--;
    20             m--;
    21         } else {
    22             nums1[i--] = nums1[m] > nums2[n] ? nums1[m--] : nums2[n--];
    23         }
    24     }
    25 };

    LeetCode 题目总结

  • 相关阅读:
    [日本语]自动词和他动词区别的重要性
    [日本语]授受关系动词总结
    [日本语]至少すくなく(と)も
    python3: 文件与IO
    python3: 迭代器与生成器(1)
    python3: 数字日期和时间(2)
    python3: 数字日期和时间(1)
    python3: 字符串和文本(4)
    Python Frame
    python3: 字符串和文本(3)
  • 原文地址:https://www.cnblogs.com/cnoodle/p/11725645.html
Copyright © 2020-2023  润新知