简体   繁体   English

合并排序数组问题 leetcode 问题 请帮我处理这段代码 我已经提到了我的一段代码和我的输入与所需输出的图像

[英]merge sorted array problem leetcode problem please help me with this code I have mentioned my piece of code and image of my input vs required output

var merge = function(nums1, m, nums2, n) { var 合并 = 函数(nums1,m,nums2,n){

let first = m-1;让第一个 = m-1;

for(let i = n-1; i>=0;i-- )
    {
        if(nums1[first]>nums2[i])
            {
                  nums1.splice[first,0,nums2[i]]
            }
        else{ 
                nums1.splice(first+1,0,nums2[i])
        }
    }
nums1.splice(m+n-1)

}; }; [enter image description here][1] [在此处输入图片说明][1]

[1]: https://i.stack.imgur.com/r2jAC.png**strong text** [1]: https : //i.stack.imgur.com/r2jAC.png**强文本**

You can use slice and concat您可以使用切片连接

 const arr1= [1,2,3,0,0] const arr2 = [2,5,6] const n1 = 3 const n2 = 3 const merge = (arr1, n1, arr2, n2) => { return arr1.splice(0, n1).concat(arr2.splice(0, n2)).sort() } console.log(merge(arr1, n1, arr2, n2))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我的javascript循环/ while语句代码不起作用,请帮助我确定我的问题 - My javascript loop/ while statement code doesn't work, please help me identify my problem 我的图像没有出现在画布上有问题,你们可以帮我吗? - i have a problem with my image not appearing on my canvas can you guys help me? 我有 html 代码,里面有 JS,我有一个问题,提交按钮没有给我警报 window,你能纠正我的代码吗? - i have html code with JS in it, i have a problem that the submit button not giving me an alert window, can you correct my code? 谁能帮助我理解为什么我的 leetcode 问题会出现这个运行时错误? 最长前缀 - Can anyone help me understand why im getting this runtime error with my leetcode problem? Longest Prefix 请帮忙,我有一个问题,我的条件不适用于我从 mongoDB.find() 获得的数据 - Please help, I have a problem, My conditional didnt work with data I gained from mongoDB.find() jQuery选项卡和超链接(请帮助我修复代码) - JQuery Tabs and Hyperlinking (help me fix my code, please) 我在 vs 代码中遇到双引号问题 - I have a problem with double quote in vs code 我的代码javascript函数onchange()有问题 - i have problem in my code javascript function onchange() 我的 JavaScript 代码以及 HTML 如何显示它有问题 - I have a problem with my JavaScript code and how HTML displays it Leetcode 问题:从有序数组中删除重复项 - Leetcode problem: Remove Duplicates from Sorted Array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM