问题描述:

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. ORM执行原生SQL语句

    # 1.connectionfrom django.db import connection, connections cursor = connection.cursor() # cursor = ...

  2. Uva1014:Remember the Word

    Neal is very curious about combinatorial problems, and now here comes a problem about words. Knowing ...

  3. Python处理URL编码

    我们在访问网站时,有很多连接都是有一些特殊符号组成,例如,我在百度搜索“大鱼海棠”,结果可以看到它的搜索出来的链接是: https://www.baidu.com/s?wd=%E5%A4%A7%E9% ...

  4. 《高性能MySQL》之MySQL查询性能优化

    为什么查询会慢? 响应时间过长.如果把查询看做是一个任务,那么它由一系列子任务组成,每个子任务都会消耗一定的时间.如果要优化查询,实际上优化其子任务,要么消除其中一些子任务,要么减少子任务的执行次数, ...

  5. Flask 笔记

    1.CBV 模式 1.继承 views.MethodView from flask.views import MethodView 2.HTTP具有 8 种请求方法 - CBV中的方法 - GET 获 ...

  6. String字符串性能优化的几种方案

    String字符串是系统里最常用的类型之一,在系统中占据了很大的内存,因此,高效地使用字符串,对系统的性能有较好的提升. 针对字符串的优化,我在工作与学习过程总结了以下三种方案作分享: 一.优化构建的 ...

  7. 解决python报错:ImportError: No module named shutil_get_terminal_size 的方法

    我的环境:Ubuntu 16.04.5 LTS 修改这个文件: $HOME/.local/lib/python2.7/site-packages/IPython/utils/terminal.py 这 ...

  8. Spring初识、新建工程

    1.spring与三层架构的关系: spring负责管理项目中的所有对象,是一个一站式的框架,容器中的对象决定了spring的功能. 2.spring核心架构 Spring框架主要由六个模块组成,在开 ...

  9. Centos 7x 安装 Telegram MTproxy代理【完美可用】

    0x00 最近迷上了Telegram,也就是电报,觉得通过这个获取国外的新闻比较方便 可是我的VPS小机子不给力,一开始使用的Centos 6x,死活装不上去, 发现MTproxy不支持Centos ...

  10. BeetleX.AdminUI介绍

    BeetleX.AdminUI是基于Beetlexjs+Vuejs+Bootstrap相结合的后台管理框架,主要介绍在不使用Webpack的情况下,如何用VS来开发一个单页面的Web后台管理应用.如果 ...