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 给两个已排序的数组,求中位数。
//我的解法比较常规,循环,把小的数添加到新数组,直到两个输入数组为空
/**
* @param {number[]} nums1
* @param {number[]} nums2
* @return {number}
*/
var findMedianSortedArrays = function(nums1, nums2) {
var arr=[];
if(nums2.length===0&&nums1.length0==0)return 0;
while(nums2.length!==0||nums1.length!==0){
if(nums1.length === 0){//数组1为空,把目标数组和数组2拼接返回给目标数组
arr = [].concat(arr,nums2);
nums2.length = 0;
}else if (nums2.length === 0) {
arr = [].concat(arr,nums1);
nums1.length = 0;
}else{//判断大小,小的删除元素并添加到目标中
arr[arr.length] = nums2[0] > nums1[0] ? nums1.shift() : nums2.shift();
}
}
return !(arr.length%2)?(arr[arr.length/2]+arr[arr.length/2-1])/2:arr[Math.floor(arr.length/2)];//返回目标数组的中位数
};

这题热门解法写的很多,而且不熟悉其语法,时间关系没有研究。留待以后吧

LeetCode解题笔记 - 4. Median of Two Sorted Arrays的更多相关文章

  1. (python)leetcode刷题笔记04 Median of Two Sorted Arrays

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

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

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

  3. LeetCode 第四题 Median of Two Sorted Arrays 二人 渣渣选手乱七八糟分析发现基本回到思路1

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

  4. 【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 sorted ...

  5. 【LeetCode】4、Median of Two Sorted Arrays

    题目等级:Hard 题目描述:   There are two sorted arrays nums1 and nums2 of size m and n respectively.   Find t ...

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

  7. leetcode 第4题 Median of Two Sorted Arrays

    class Solution { public: double findMedianSortedArrays(vector<int>& nums1, vector<int&g ...

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

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

  9. 4. Median of Two Sorted Arrays(topK-logk)

    4. Median of Two Sorted Arrays 题目 There are two sorted arrays nums1 and nums2 of size m and n respec ...

随机推荐

  1. Flask 教程 第十三章:国际化和本地化

    本文翻译自The Flask Mega-Tutorial Part XIII: I18n and L10n 这是Flask Mega-Tutorial系列的第十三部分,我将告诉你如何扩展Microbl ...

  2. sleuth和zipkin微服务里的链路跟踪

    分布式链路跟踪介绍 对于一个微服务系统,大多数来自外部的请求都会经过数个服务的互相调用,得到返回的结果,一旦结果回复较慢或者返回了不可用,我们就需要确定是哪个微服务出了问题.于是就有了分布式系统调用跟 ...

  3. Navicat远程连接MySQL8,必知防坑策略

    项目上线是每一个开发工程师面临收获前面抓紧时间开发的成果,但有时我们上线项目首先需要做一些相关的业务测试.通过Xshell远程连接后使用命令行的方式连接操作Mysql这个没什么太大的你问题.但每次通过 ...

  4. Java_JDBC 连接

    今天,接着上一篇( mysql 数据库 )的基础上,我就写一下 Java 怎样连接数据库,并且操作数据库. 首先,我们先来准备一下数据库连接的驱动: mysql 的 jar 包下载地址:https:/ ...

  5. AWS SNS 创建 订阅 发布

    AWS SNS 创建 订阅 发布 20180810 chenxin 为实现短信报警,添加以下SNS的短信(SMS)订阅 选择主题,创建新主题,或修改原有主题 进入对应主题后,选择创建订阅,选择SMS, ...

  6. chunked

    简介 chunked是一种HTTP协议的分块传输编码的机制,即一个HTTP消息可以分成多个部分进行传输,它对于HTTP请求和HTTP响应都适用.对于非持续连接,浏览器通过连接是否关闭来界定请求和响应实 ...

  7. 【cf932E】E. Team Work(第二类斯特林数)

    传送门 题意: 求\(\displaystyle \sum_{i=0}^n{n\choose i}i^k,n\leq 10^9,k\leq 5000\). 思路: 将\(i^k\)用第二类斯特林数展开 ...

  8. VirtualBox中重建Host-Only网卡后无法启动虚拟机

    问题: 在删除原有VirtualBox Host-Only虚拟网卡并重新添加后,虚拟机可能会无法启动,出现以下错误 Failed to open/create the internal network ...

  9. linux-在指定路径下查询文件夹是否存在

    我们常常在Linux下去查找文件 find / -name 'test.py' # 在根目录下查找名为test.py的文件 但是如果用查找文件的方式去查找文件夹的话,是查不到的 find / -max ...

  10. Eureka工作原理及它和ZooKeeper的区别

    1.Eureka 简介: Eureka 是 Netflix 出品的用于实现服务注册和发现的工具. Spring Cloud 集成了 Eureka,并提供了开箱即用的支持.其中, Eureka 又可细分 ...