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. 小程序如何实现rem

    最近在学习小程序,要把html的代码转换成小程序界面,其中就遇到了rem的转换问题,但小程序不太兼容rem,不是不能用rem,而是没办法设置根元素的font-size,因为rem是相对于根元素的fon ...

  2. PlayJava Day027

    进程状态 1.创建状态:在程序中用构造方法创建了一个线程对象后,新的线程对象便处于新建状态 此时,它已经有了相应的内存空间和其他资源,但还处于不可运行状态 新建一个线程对象可采用Thread类的构造方 ...

  3. Ubuntu Server 上使用Docker Compose 部署Nexus(图文教程)

    场景 Docker-Compose简介与Ubuntu Server 上安装Compose: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/deta ...

  4. C/C++ 中的头文件 stdio.h和stdlib.h

    stdio 就是指 “standard input & output" 标准输入输出 stdio.h所包含的函数: 文件访问fopenfreopenfflushfclose二进制输入 ...

  5. 【Java基础】接口和抽象类之间的对比

    Java 中的接口和抽象类之间的对比 一.接口 Interface,将其翻译成插座可能就更好理解了.我们通常利用接口来定义实现类的行为,当你将插座上连接笔记本的三角插头拔掉,换成微波炉插上去的时候,你 ...

  6. ORA-17627: ORA-12577:关于文件存储满的问题

    问题描述:搭建DG的时候,要rman从orcl恢复到orclstd数据库来,dup复制了半天,结果最后报错:ORA-17627: ORA-12577: Message 12577 not found; ...

  7. KubeSphere and Friends|12 月 14 日相约北京,不见不散

    如今在容器圈提到 Kubernetes,可谓是无人不知无人不晓.KubeSphere 作为一款面向云原生设计的开源项目,目的是在 Kubernetes 之上构建分布式多租户容器管理平台,提供简单易用的 ...

  8. 多对多表结构的设计ManyToManyField(不会生成某一列、生成一张表):

    示例: 脚本: from django.db import models# Create your models here. class Publisher(models.Model): name = ...

  9. (day65、66)Vue基础、指令、实例成员、JS函数this补充、冒泡排序

    目录 一.Vue基础 (一)什么是Vue (二)为什么学习Vue (三)如何使用Vue 二.Vue指令 (一)文本指令 (二)事件指令v-on (三)属性指令v-bind (四)表单指令v-model ...

  10. vsftpd服务的基本配置

    本文环境:CentOS 7 简介 FTP(文件传输协议,File Transfer Protocol)是最古老的协议之一,诞生于1971年,距今已经半个世纪了,它的目的是在不同计算机之间传输文件(实现 ...