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
给你两个有序整数数组 nums1 和 nums2,请你将 nums2 合并到 nums1 中,使 num1 成为一个有序数组。
输入: nums1 = [1,2,3,0,0,0], m = 3 nums2 = [2,5,6], n = 3
输出: [1,2,2,3,5,6]
因为合并后的有效长度是m+n,且nums1有足够的空间,所以采用从后面开始填充数据;循环的终止条件是nums2的各项都添加到nums1了,但是要考虑到m为0的时候得情况
/** * @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) { var sum = m + n - 1; while(n>0){ if(m==0){ nums1[sum] = nums2[n-1] sum--; n-- }else if(nums1[m-1]>nums2[n-1]){ nums1[sum] = nums1[m-1] sum--; m-- }else{ nums1[sum] = nums2[n-1] sum--; n-- } } };
The text was updated successfully, but these errors were encountered:
No branches or pull requests
题目描述
给你两个有序整数数组 nums1 和 nums2,请你将 nums2 合并到 nums1 中,使 num1 成为一个有序数组。
说明:
示例:
输出: [1,2,2,3,5,6]
分析
因为合并后的有效长度是m+n,且nums1有足够的空间,所以采用从后面开始填充数据;循环的终止条件是nums2的各项都添加到nums1了,但是要考虑到m为0的时候得情况
The text was updated successfully, but these errors were encountered: