From 510d6e6267de6fde5326962286c320a818f574f9 Mon Sep 17 00:00:00 2001 From: Hassan Mehmood Date: Fri, 22 Nov 2024 06:02:40 +0500 Subject: [PATCH] Time: 0 ms (100%), Space: 49.5 MB (13.03%) - LeetHub --- .../0088-merge-sorted-array.js | 23 ++++++++++++------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/0088-merge-sorted-array/0088-merge-sorted-array.js b/0088-merge-sorted-array/0088-merge-sorted-array.js index 25fd86f..6c3ebc1 100644 --- a/0088-merge-sorted-array/0088-merge-sorted-array.js +++ b/0088-merge-sorted-array/0088-merge-sorted-array.js @@ -6,12 +6,19 @@ * @return {void} Do not return anything, modify nums1 in-place instead. */ var merge = function (nums1, m, nums2, n) { - let i = 0 - while (i <= nums2.length - 1) { - nums1[m] = nums2[i] - m++ - i++ + let l = 0 + for (let i = nums1.length - 1; i >= 0; i--) { + if (nums2[l] == undefined) { + break + } + if (nums1[i] == 0) { + nums1[i] = nums2[l] + l++ + } + else { + break + } } - return nums1.sort((a, b) => a - b) - -}; \ No newline at end of file + nums1 = nums1.sort((a, b) => a - b) + return nums1.length +};