Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✅88. 合并两个有序数组 #72

Open
bazinga-web opened this issue Jul 26, 2020 · 1 comment
Open

✅88. 合并两个有序数组 #72

bazinga-web opened this issue Jul 26, 2020 · 1 comment
Labels

Comments

@bazinga-web
Copy link

88. 合并两个有序数组

给你两个有序整数数组 nums1 和 nums2,请你将 nums2 合并到 nums1 中,使 nums1 成为一个有序数组。

 

说明:

初始化 nums1 和 nums2 的元素数量分别为 m 和 n 。
你可以假设 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]
@Ray-56
Copy link
Owner

Ray-56 commented Jul 27, 2020

从后往前判断赋值

/**
 * @param {number[]} nums1
 * @param {number} m
 * @param {number[]} nums2
 * @param {number} n
 * @return {void} Do not return anything, modify nums1 in-place instead.
 */
var merge = function(nums1, m, nums2, n) {
    let count = m + n;
    while (m > 0 && n > 0) {
        nums1[--count] = nums1[m - 1] > nums2[n - 1] ? nums1[--m] : nums2[--n];
    }
    if (n > 0) {
        nums1.splice(0, n, ...nums2.slice(0, n));
    }
};

@Ray-56 Ray-56 added the 简单 label Jul 27, 2020
@Ray-56 Ray-56 changed the title 88. 合并两个有序数组 ✅88. 合并两个有序数组 Jul 27, 2020
@Ray-56 Ray-56 changed the title ✅88. 合并两个有序数组 88. 合并两个有序数组 Jul 28, 2020
@Ray-56 Ray-56 changed the title 88. 合并两个有序数组 ✅88. 合并两个有序数组 Aug 13, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants