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. Provide Several View Variants for End-Users 为最终用户提供多个视图变体

    In this lesson, you will learn how to provide several customized variants of the same View, and allo ...

  2. Filter List Views 筛选器列表视图

    In this lesson, you will learn how to filter a List View. Three techniques, based on different scena ...

  3. Class文件结构-实例学习笔记

  4. LINE 项目总结

    前段时间做了一个“国外的公众号”,为什么说是国外的公众号呢,因为他和微信的公众号有很多共通之处.当然了也有很多不同的地方,第一个要说的就是,我在打印信息的时候,使用的alert()弹窗,但是后台才发现 ...

  5. layui table 表格查询无效问题

    [热身话题] 在开发的过程中,大量数据的展示大多采用表格的方式,直观,清晰.在这里,我也使用过一些框架Bootstrap.table ,Dev table ,layui table.本次采用的layu ...

  6. InnoDB On-Disk Structures(二)--Indexes (转载)

    转载.节选于 https://dev.mysql.com/doc/refman/8.0/en/innodb-indexes.html This section covers topics relate ...

  7. [PHP] 编译安装swoole

    不知道为啥,家里的网访问不了pecl.php.net,这也就没有办法直接使用pecl install swoole安装swoole,也可以直接进行编译安装 在git仓库下载源码 https://git ...

  8. Spring Security从后台数据库查询实现登陆控制

    Spring Security框架是一个控制登陆的框架,通过配置文件获取后台的用户名及密码,进行比较进行登陆判断 使用步骤 1.导入依赖 <!-- 身份验证 --> <depende ...

  9. GitLab CI/CD持续集成设置

    GitLab CI/CD持续设置 官方文档地址(https://docs.gitlab.com/ee/ci/README.html) GitLab CI.CD功能非常完善,只需要简单几步,就可以完成项 ...

  10. Electron桌面项目-解决throw new Error('Electron failed to install correctly, please delete node_modules..

    前言 Electron 是一个用 HTML,CSS 和 JavaScript 来构建跨平台桌面应用程序的一个开源库.由GitHub开发的. 其原理是 Electron 通过将 Chromium 和 N ...