admin管理员组文章数量:1026989
var merge = function(nums1, m, nums2, n) {
//contcating two array
let array = nums2.concat(nums1)
// sort the array
array.sort((a,b) => a-b)
// remove element > m+n length
return array.slice(m+n-n)
};
This ^ function is returning -> [1,2,3,0,0,0] if i'm applying console then answer is same as expected -> [1,2,2,3,5,6]
Why is this happening ?
var merge = function(nums1, m, nums2, n) {
//contcating two array
let array = nums2.concat(nums1)
// sort the array
array.sort((a,b) => a-b)
// remove element > m+n length
return array.slice(m+n-n)
};
This ^ function is returning -> [1,2,3,0,0,0] if i'm applying console then answer is same as expected -> [1,2,2,3,5,6]
Why is this happening ?
Share Improve this question edited Jun 7, 2022 at 6:11 Hritik Sharma 2,0161 gold badge11 silver badges27 bronze badges asked Jun 7, 2022 at 5:46 ma_12ma_12 751 silver badge6 bronze badges 5-
What's the input?
num1
,m
,num2
andn
? – zer00ne Commented Jun 7, 2022 at 5:50 - nums1 ->[1,2,3,0,0,0] nums2 ->[2,5,6] n , m -> 3 – ma_12 Commented Jun 7, 2022 at 5:56
-
So
m
andn
are both 3? The last line you are removing index 3+3-3 ...soslice(3)
will return an array without the first 3 numbers...why? and why not justm
ifn-n
? – zer00ne Commented Jun 7, 2022 at 6:11 -
m + n - n
= m !? do you meanarray.slice(m+n,n)
– Xupitan Commented Jun 7, 2022 at 6:16 -
with 2 arrays :
[1,2,3.0,0,0]
and[2,5,6]
. result is[1,2,2,3,5,6]
, must bearray.slice(3)
orarray.slice(3, array.length)
– Xupitan Commented Jun 7, 2022 at 6:51
5 Answers
Reset to default 4Remove
slice function from the end of the function.
slice(m+n-n)
slices your sorted array and returns array from index m+1 to the last index.
var merge = function(nums1, m, nums2, n) {
//contcating two array
let array = nums2.concat(nums1)
// sort the array
array.sort((a,b) => a-b)
// remove element > m+n length
return array.slice(m+n-n);
};
console.log(merge([2,4,8,9],4,[0,4,6,9],4));
You can use the following function to merge and then sort the two arrays.
Time plexity of this approach is O(nlogn)
function merge(arr1,arr2){
return [...arr1,...arr2].sort();
}
console.log(merge([4,8,6],[1,3,9,10]));
The second approach runs in O(n)
time.
function merge(arr1,m,arr2,n){
let result = [];
let i=0 , j = 0 ;
while(i<m && j<n){
if(arr1[i]<arr2[j]){
result.push(arr1[i]);
i++;
}else{
result.push(arr2[j]);
j++;
}
}
while(i<m){
result.push(arr1[i]);
i++;
}
while(j<n){
result.push(arr2[j]);
j++;
}
return result;
}
console.log(merge([4,5,6],3,[1,3,8,10],4));
It is mentioned that Do not return anything, modify nums1 in-place instead., So the output is your nums1 array and not array which you have created.
Note: To resolve this issue you can modify the nums1 array, below is the sample code:
var merge = function(nums1, m, nums2, n) {
const index = nums1.length - n;
for(let i=index;i < nums1.length;i++) {
nums1[i] = nums2[i-index];
}
nums1.sort((a,b)=> a-b);
};
I did not see any problem with your code and it works, I just added another code. I think the problem with your console.log
.
var merge = function(nums1, m, nums2, n) {
//contcating two array
let array = nums2.concat(nums1)
// sort the array
array.sort((a,b) => a-b)
// remove element > m+n length
return array.slice(m+n-n)
};
console.log(merge([1,2,3,0,0,0],3,[2,5,6],3));
var merge = function(nums1, m, nums2, n) {
return nums1.slice(0, m)
.concat(nums2.slice(0, n))
.sort((i, j) => i - j);
};
console.log( merge([1,2,3,0,0,0],3,[2,5,6],3))
By using simple for loop
let arr1 = [10, 20, 30, 45, 50]
let arr2 = [15, 25, 26, 35]
const mergeArray = (arr1, arr2) => {
let arr3 = []
let i = 0;
let j = 0;
for (let n = 0; n < arr1.length + arr2.length; n++) {
if (arr1[i] <= arr2[j] || arr2.length < j + 1) {
arr3.push(arr1[i])
i++
} else {
arr3.push(arr2[j])
j++
}
}
return arr3
}
console.log(JSON.stringify(mergeArray(arr1, arr2)))
I tried to use a two-pointer algorithm but realized that I need 3 pointers in this case.
var merge = function(nums1, m, nums2, n) {
let i = m - 1; // Pointer for the last initialized element in nums1
let j = n - 1; // Pointer for the last element in nums2
let k = m + n - 1; // Pointer for the last position in nums1
// Iterate backwards and fill nums1 from the end
while (j >= 0) {
if (i >= 0 && nums1[i] > nums2[j]) {
nums1[k] = nums1[i];
i--;
} else {
nums1[k] = nums2[j];
j--;
}
k--;
}
return nums1;
};
var merge = function(nums1, m, nums2, n) {
//contcating two array
let array = nums2.concat(nums1)
// sort the array
array.sort((a,b) => a-b)
// remove element > m+n length
return array.slice(m+n-n)
};
This ^ function is returning -> [1,2,3,0,0,0] if i'm applying console then answer is same as expected -> [1,2,2,3,5,6]
Why is this happening ?
var merge = function(nums1, m, nums2, n) {
//contcating two array
let array = nums2.concat(nums1)
// sort the array
array.sort((a,b) => a-b)
// remove element > m+n length
return array.slice(m+n-n)
};
This ^ function is returning -> [1,2,3,0,0,0] if i'm applying console then answer is same as expected -> [1,2,2,3,5,6]
Why is this happening ?
Share Improve this question edited Jun 7, 2022 at 6:11 Hritik Sharma 2,0161 gold badge11 silver badges27 bronze badges asked Jun 7, 2022 at 5:46 ma_12ma_12 751 silver badge6 bronze badges 5-
What's the input?
num1
,m
,num2
andn
? – zer00ne Commented Jun 7, 2022 at 5:50 - nums1 ->[1,2,3,0,0,0] nums2 ->[2,5,6] n , m -> 3 – ma_12 Commented Jun 7, 2022 at 5:56
-
So
m
andn
are both 3? The last line you are removing index 3+3-3 ...soslice(3)
will return an array without the first 3 numbers...why? and why not justm
ifn-n
? – zer00ne Commented Jun 7, 2022 at 6:11 -
m + n - n
= m !? do you meanarray.slice(m+n,n)
– Xupitan Commented Jun 7, 2022 at 6:16 -
with 2 arrays :
[1,2,3.0,0,0]
and[2,5,6]
. result is[1,2,2,3,5,6]
, must bearray.slice(3)
orarray.slice(3, array.length)
– Xupitan Commented Jun 7, 2022 at 6:51
5 Answers
Reset to default 4Remove
slice function from the end of the function.
slice(m+n-n)
slices your sorted array and returns array from index m+1 to the last index.
var merge = function(nums1, m, nums2, n) {
//contcating two array
let array = nums2.concat(nums1)
// sort the array
array.sort((a,b) => a-b)
// remove element > m+n length
return array.slice(m+n-n);
};
console.log(merge([2,4,8,9],4,[0,4,6,9],4));
You can use the following function to merge and then sort the two arrays.
Time plexity of this approach is O(nlogn)
function merge(arr1,arr2){
return [...arr1,...arr2].sort();
}
console.log(merge([4,8,6],[1,3,9,10]));
The second approach runs in O(n)
time.
function merge(arr1,m,arr2,n){
let result = [];
let i=0 , j = 0 ;
while(i<m && j<n){
if(arr1[i]<arr2[j]){
result.push(arr1[i]);
i++;
}else{
result.push(arr2[j]);
j++;
}
}
while(i<m){
result.push(arr1[i]);
i++;
}
while(j<n){
result.push(arr2[j]);
j++;
}
return result;
}
console.log(merge([4,5,6],3,[1,3,8,10],4));
It is mentioned that Do not return anything, modify nums1 in-place instead., So the output is your nums1 array and not array which you have created.
Note: To resolve this issue you can modify the nums1 array, below is the sample code:
var merge = function(nums1, m, nums2, n) {
const index = nums1.length - n;
for(let i=index;i < nums1.length;i++) {
nums1[i] = nums2[i-index];
}
nums1.sort((a,b)=> a-b);
};
I did not see any problem with your code and it works, I just added another code. I think the problem with your console.log
.
var merge = function(nums1, m, nums2, n) {
//contcating two array
let array = nums2.concat(nums1)
// sort the array
array.sort((a,b) => a-b)
// remove element > m+n length
return array.slice(m+n-n)
};
console.log(merge([1,2,3,0,0,0],3,[2,5,6],3));
var merge = function(nums1, m, nums2, n) {
return nums1.slice(0, m)
.concat(nums2.slice(0, n))
.sort((i, j) => i - j);
};
console.log( merge([1,2,3,0,0,0],3,[2,5,6],3))
By using simple for loop
let arr1 = [10, 20, 30, 45, 50]
let arr2 = [15, 25, 26, 35]
const mergeArray = (arr1, arr2) => {
let arr3 = []
let i = 0;
let j = 0;
for (let n = 0; n < arr1.length + arr2.length; n++) {
if (arr1[i] <= arr2[j] || arr2.length < j + 1) {
arr3.push(arr1[i])
i++
} else {
arr3.push(arr2[j])
j++
}
}
return arr3
}
console.log(JSON.stringify(mergeArray(arr1, arr2)))
I tried to use a two-pointer algorithm but realized that I need 3 pointers in this case.
var merge = function(nums1, m, nums2, n) {
let i = m - 1; // Pointer for the last initialized element in nums1
let j = n - 1; // Pointer for the last element in nums2
let k = m + n - 1; // Pointer for the last position in nums1
// Iterate backwards and fill nums1 from the end
while (j >= 0) {
if (i >= 0 && nums1[i] > nums2[j]) {
nums1[k] = nums1[i];
i--;
} else {
nums1[k] = nums2[j];
j--;
}
k--;
}
return nums1;
};
本文标签: javascriptMerge Sorted Array leetcodeStack Overflow
版权声明:本文标题:javascript - Merge Sorted Array leetcode - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745666065a2162184.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论