问题描述:

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)).

Example 1:

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

Example 2:

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

问题思路:

(1)本题不知道为啥难度级别是hard,但是对于使用js来说,真的挺好做的。可能我没有考虑到什么算法复杂度,还有就是js已经封装好sort算法了。

(2)很自然的想到将nums1 和 nums2 数组组成一个数组,并按序排列,然后找出中值。

(3)js提供扩展运算符或concat,迅速将两个数组组成一个数组;然后使用sort()进行排序

code:

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--js--Median of Two Sorted Arrays的更多相关文章

  1. 【算法之美】求解两个有序数组的中位数 — leetcode 4. Median of Two Sorted Arrays

    一道非常经典的题目,Median of Two Sorted Arrays.(PS:leetcode 我已经做了 190 道,欢迎围观全部题解 https://github.com/hanzichi/ ...

  2. LeetCode(3) || Median of Two Sorted Arrays

    LeetCode(3) || Median of Two Sorted Arrays 题记 之前做了3题,感觉难度一般,没想到突然来了这道比较难的,星期六花了一天的时间才做完,可见以前基础太差了. 题 ...

  3. LeetCode 4 Median of Two Sorted Arrays (两个数组的mid值)

    题目来源:https://leetcode.com/problems/median-of-two-sorted-arrays/ There are two sorted arrays nums1 an ...

  4. Leetcode 4. Median of Two Sorted Arrays(二分)

    4. Median of Two Sorted Arrays 题目链接:https://leetcode.com/problems/median-of-two-sorted-arrays/ Descr ...

  5. LeetCode 4. Median of Two Sorted Arrays & 归并排序

    Median of Two Sorted Arrays 搜索时间复杂度的时候,看到归并排序比较适合这个题目.中位数直接取即可,所以重点是排序. 再来看看治阶段,我们需要将两个已经有序的子序列合并成一个 ...

  6. 第三周 Leetcode 4. Median of Two Sorted Arrays (HARD)

    4. Median of Two Sorted Arrays 给定两个有序的整数序列.求中位数,要求复杂度为对数级别. 通常的思路,我们二分搜索中位数,对某个序列里的某个数 我们可以在对数时间内通过二 ...

  7. Leetcode 4. Median of Two Sorted Arrays(中位数+二分答案+递归)

    4. Median of Two Sorted Arrays Hard There are two sorted arrays nums1 and nums2 of size m and n resp ...

  8. LeetCode 004 Median of Two Sorted Arrays

    题目描述:Median of Two Sorted Arrays There are two sorted arrays A and B of size m and n respectively. F ...

  9. leetcode 4. Median of Two Sorted Arrays

    https://leetcode.com/problems/median-of-two-sorted-arrays/ There are two sorted arrays nums1 and num ...

  10. leetcode之 median of two sorted arrays

    这是我做的第二个leetcode题目,一开始以为和第一个一样很简单,但是做的过程中才发现这个题目非常难,给人一种“刚上战场就踩上地雷挂掉了”的感觉.后来搜了一下leetcode的难度分布表(leetc ...

随机推荐

  1. Java入门 - 语言基础 - 20.Stream和File和IO

    原文地址:http://www.work100.net/training/java-stream-file-io.html 更多教程:光束云 - 免费课程 Stream和File和IO 序号 文内章节 ...

  2. Flsak学习笔记(1)

    Day 01 最近项目里要用python写后端,同学推荐了flask框架就来学一学.写这个博客的目的主要是记录一下自己学习的内容,有基础知识忘了不用一个个去百度,还有就是跟大家分享一下,有不是很容易理 ...

  3. 机器学习-MNIST数据集使用二分类

    一.二分类训练MNIST数据集练习 %matplotlib inlineimport matplotlibimport numpy as npimport matplotlib.pyplot as p ...

  4. mongo 查询 距离 某个点 多少 米距离 感谢 提供的数据。 感谢 mvc的 demo 。反正 就是各种感谢 文档之类的。

    昨天 去面试来着, 问了一下mong . 我记得mong支持 地理位置索引的,说了一下. 然后 面试官说 查询某个点 的 多少米范围, 这个该怎么实现? 我懵逼了.... 回去 查询了一下. 发现有 ...

  5. HGE引擎改进——2014/2/18 和 2014/2/27

    2014/2/18 更新 hgehelper库:增加hgeSkeleton类,该类用于播放骨骼动画 增加工具骨骼动画编辑器(AnimationEd),该工具用于骨骼动画的编辑 2014/2/27 更新 ...

  6. sqli_labs学习笔记(一)Less-38~Less-53

    续上,开门见山 堆叠注入,实际上就构成了两条SQL语句 http://43.247.91.228:84/Less-38/?id=1' union select 1,2,3 --+    //未报错 h ...

  7. pythonenv的安装及迁移

    一.安装 运行 pip install virtualenv 即可安装virtualenv,想用 最新开发版 就运行 二. virtualenv基本使用 $ python virtualenv.py ...

  8. rtmp协议规范

    译序: 本文是为截至发稿时止最新 Adobe 官方公布的 RTMP 规范.本文包含 RTMP 规范的全部内容.是第一个比较全面的 RTMP 规范的中译本.由于成文时间仓促,加上作者知识面所限,翻译错误 ...

  9. apache 目录网站显示indexs

    Apache Options Indexes FollowSymLinks详解 第一种方法 <Directory "E:\myvirtualhost\localhost"&g ...

  10. Jconsole或者VisualVM监控远程主机(阿里云,jdk11或者8)

    准备: 1 一个war包或者jar包,这里我用springboot的 2 linux环境,安装tomcat,jdk,我用的jdk11和tomcat9,jdk11和8的拷贝权限文件路径有点不一样,这个需 ...