We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
原题链接:https://leetcode-cn.com/problems/merge-sorted-array/
解题思路:
/** * @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 tempArr = []; // 用于缓存排序后结果 let index1 = 0; // 用于遍历nums1。 let index2 = 0; // 用于遍历nums2。 // 使用双指针遍历,遍历完任意一个数组的有效值后退出。 while (index1 < m && index2 < n) { // 选取两个数组中较小的值存入新数组。 if (nums1[index1] <= nums2[index2]) { tempArr.push(nums1[index1++]); } else { tempArr.push(nums2[index2++]); } } // 检测哪个数组还有值没有保存,将剩余的值存入新数组。 if (index1 < m) { tempArr.splice(index1 + n, m - index1, ...nums1.slice(index1, m)); } if (index2 < n) { tempArr.splice(index2 + m, n - index2, ...nums2.slice(index2, n)); } // 将排序后结果存储到nums1 nums1.splice(0, m + n, ...tempArr); };
The text was updated successfully, but these errors were encountered:
No branches or pull requests
原题链接:https://leetcode-cn.com/problems/merge-sorted-array/
解题思路:
The text was updated successfully, but these errors were encountered: