Degree

Easy ★ (*´╰╯`๓)♬

Description:

There are two sorted arrays nums1 and nums2 of size m and n respectively.
Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).

Example1:

nums1 = [1, 3]
nums2 = [2]The median is 2.0

Example2:

nums1 = [1, 2]
nums2 = [3, 4]The median is (2 + 3)/2 = 2.5

Ideas

  • 将两个数组合并成一个数组并按大小顺序排序
  • 选取数组的中间两个数或一个数
  • 算出平均数

Code

/*** @param {number[]} nums1* @param {number[]} nums2* @return {number}*/
var findMedianSortedArrays = function(nums1, nums2) {function compare(val1,val2){return val1-val2;};let nums3 = nums1.concat(nums2).sort(compare);let lowMiddle = Math.floor((nums3.length - 1) / 2);let highMiddle = Math.ceil((nums3.length - 1) / 2);let median = (nums3[lowMiddle] + nums3[highMiddle]) / 2;return median;
};

转载于:https://www.cnblogs.com/Iris-mao/p/8716804.html

leetcode - 4. Median of Two Sorted Arrays相关推荐

  1. 【leetcode】Median of Two Sorted Arrays

    题目简述: There are two sorted arrays A and B of size m and n respectively. Find the median of the two s ...

  2. LeetCode 4 Median of Two Sorted Arrays

    There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...

  3. LeetCode 4. Median of Two Sorted Arrays

    题目: There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the ...

  4. LeetCode.004 Median of Two Sorted Arrays

    There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...

  5. LeetCode | 4. Median of Two Sorted Arrays(中位数)

    题目 There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the ...

  6. java打乱一组正序数字,Leetcode︱4.Median of Two Sorted Arrays寻找两个正序数组的中位数.java...

    题目 给定两个大小分别为 m 和 n 的正序(从小到大)数组 nums1 和 nums2.请你找出并返回这两个正序数组的 中位数 . 示例 : 输入:nums1 = [1,3], nums2 = [2 ...

  7. leetcode 之Median of Two Sorted Arrays(五)

    找两个排好序的数组的中间值,实际上可以扩展为寻找第k大的数组值. 参考下面的思路,非常的清晰: 代码: double findMedianofTwoSortArrays(int A[], int B[ ...

  8. 【LeetCode】004 Median of Two Sorted Arrays 两个排序数组合并后的中位数

    题目:LeetCode 004 Median of Two Sorted Arrays There are two sorted arrays nums1 and nums2 of size m an ...

  9. Kotlin实现LeetCode算法题之Median of Two Sorted Arrays

    题目Median of Two Sorted Arrays(难度Hard) 方案1,数组合并&排序调用Java方法 1 import java.util.* 2 3 class Solutio ...

最新文章

  1. 【OpenCV 4开发详解】Scharr算子
  2. 搜python编程题_100+Python编程题给你练~(附答案)
  3. 浙江金华暂时停用“智能头箍”,专家:监测学生脑电违反伦理
  4. request.getRealPath不推荐使用
  5. 面向对象编程 object oriented programming(OOP)(第二篇)
  6. 加密数据包--加解密部分逆向跟踪
  7. 编写DLL所学所思(1)——导出函数
  8. Java设计模式学习总结(2)——创建型模式之工厂模式
  9. [快速入门]Spring Boot+springfox-swagger2 之RESTful API自动生成和测试
  10. react生命周期钩子
  11. BZOJ 3509 分块FFT
  12. stm32气压传感器 带探头的_几种常用传感器
  13. C4D模型工具—恢复平滑着色(Phong)
  14. 基于ffmpeg+opencv的h264解码显示功能的实现
  15. python数据化运营案例---简单销售预测案例(线性回归模型)
  16. 人工智能进化史:从麦卡锡到“索菲亚” | 精选
  17. ESP-AT 应用: AT+MQTT 对接腾讯 QCloud 云
  18. Probability|Given UVA - 11181
  19. iOS 视频播放(AVPlayer)
  20. unittest用例封装

热门文章

  1. JavaScript基础和Web APIs两个阶段的关联性(1)
  2. android定位地点的保存,Android获得所有存储设备位置的最佳方法
  3. 雷电模拟器多开cpu优化_哪个电脑手游模拟器好用 安卓手游模拟器测试对比排行榜...
  4. 2020蓝桥杯省内模拟赛C++B组1-8(详细解析,看完就会)
  5. 递归法:走楼梯; 旋转数组的最小数字(递归法和改进二分法)
  6. 官网opencv练习题 最简单的多物体分离技术
  7. [debug] “ImportError DLL load failed 找不到指定的程序”的解析和解决办法。
  8. 环,模,补码,负数的表示与减法实现
  9. 轻松三步教你配置oracle,Oracle Net Configuration Assistant 配置步骤简明教程
  10. 6. 添加权限管理模块