Median of Two Sorted Arrays(leetcode)
程序员文章站
2022-05-14 20:20:15
...
1.js将两个数组合为一个数组的方法可以使用concat()
,也可以使用扩展运算符
,所以方法就有两种
var arr = nums1.concat(nums2).sort((a,b)=>a-b);
var a = (nums1.length + nums2.length)%2;
var b = (nums1.length + nums2.length)/2;
if(a == 0){
return (arr[b-1]+arr[b])/2;
}else{
b = Math.floor(b);
return arr[b];
}
或者是
var findMedianSortedArrays = function(nums1, nums2) {
var arr = [...nums1, ...nums2].sort((a,b)=>a-b);
var a = (nums1.length + nums2.length)%2;
var b = (nums1.length + nums2.length)/2;
if(a == 0){
return (arr[b-1]+arr[b])/2;
}else{
b = Math.floor(b);
return arr[b];
}
};
推荐阅读
-
[LeetCode] 004. Median of Two Sorted Arrays (Hard) 经典分治
-
【LeetCode】Two Sum & Two Sum II - Input array is sorted & Two Sum IV - Input is a BST
-
LeetCode 21. 合并两个有序链表 Merge Two Sorted Lists
-
[LeetCode]Merge Two Sorted Lists(Python)
-
【LeetCode】4. Median of Two Sorted Arrays
-
1.Merge Two Sorted Arrays. py/合并排序有序数列
-
LeetCode 4. 两个排序数组的中位数 Median of Two Sorted Arrays
-
算法练习(3):Median of Two Sorted Arrays
-
LeetCode算法系列:4、Median of Two Sorted Arrays
-
4. Median of Two Sorted Arrays