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 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的更多相关文章
- (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 ...
- Kotlin实现LeetCode算法题之Median of Two Sorted Arrays
题目Median of Two Sorted Arrays(难度Hard) 方案1,数组合并&排序调用Java方法 import java.util.* class Solution { fu ...
- 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 ...
- 【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 ...
- 【LeetCode】4、Median of Two Sorted Arrays
题目等级:Hard 题目描述: There are two sorted arrays nums1 and nums2 of size m and n respectively. Find t ...
- 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 ...
- leetcode 第4题 Median of Two Sorted Arrays
class Solution { public: double findMedianSortedArrays(vector<int>& nums1, vector<int&g ...
- 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. 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 ...
随机推荐
- ribbon客户端负载均衡
Ribbon简介 参考:https://blog.csdn.net/chengqiuming/article/details/80711168 Ribbon是Netflix发布的负载均衡器,它有助于控 ...
- How to: Implement a Custom Base Persistent Class 如何:实现自定义持久化基类
XAF ships with the Business Class Library that contains a number of persistent classes ready for use ...
- MyBatis映射文件 相关操作
一.MyBatis映射文件 1.简介 MyBatis 的真正强大在于它的映射语句,也是它的魔力所在.由于它的异常强大,映射器的 XML 文件就显得相对简单.如果拿它跟具有相同功能的 JDBC 代码进行 ...
- c#使用CefSharp开发winform——环境搭建
因为nuget在线下载有时候很慢,所以下载nuget包,进行本地安装 登陆nuget官网https://www.nuget.org/下载相关资源 选择对应版本进行下载,同时需要下载相关依赖的包 最 ...
- Odoo,快速上手Odoo,来了解Odoo几个标准模块
odoo通过Apps和Connector扩展和集成数以万计的应用和服务,odoo目前有超过12500个Apps可选用.它囊括了项目管理,生产.财务.记账和销售管理,仓储管理,人力资源管理,等等项目.本 ...
- HTTP Error 500.19 - Internal Server Error 无法读取配置文件
将Code移动文件夹就报以下错误,http error 500.19 - internal server error. 项目文件下.vs=>config=>applicationhost. ...
- 误删mysql root账户
看这个账户有点碍眼就删除了,结果异常了mysql> select user,host from mysql.user;+---------------+-------------+| user ...
- PHP计算二维数组指定元素的和
array_sum(array_column($arr, 'num')); //计算二维数组指定元素的和 $arr = [ [ 'id'=>1, 'num'=>3, ], [ 'id'=& ...
- Markdown数学公式语法
详细网址:Markdown数学公式语法
- 编码方式ASCII、GBK、Unicode、UTF-8比较
文章内容深度较浅,详细了解可到下链接:https://blog.csdn.net/QuinnNorris/article/details/78705723; 总结了以下几种编码方式: ASCII.GB ...