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 ...
随机推荐
- Flask 教程 第十章:邮件支持
本文翻译自The Flask Mega-Tutorial Part X: Email Support 这是Flask Mega-Tutorial系列的第十部分,在其中我将告诉你,应用如何向你的用户发送 ...
- Java学习 1.1——(JVM介绍)Java为什么能够跨平台?
首先介绍一下Java的各个层级,先放一张图: 硬件,操作系统和操作系统接口:这三级不说大家都知道,操作系统有很多种,比如Windows,Linux.Windows又分为win7,win10,win x ...
- 【坑】The server time zone value '�й���ʱ��' is unrecognized or represents more than one time zone.
在使用spring JDBC 连接数据库时出现的错误: Caused by: com.mysql.cj.exceptions.InvalidConnectionAttributeException: ...
- JS---DOM---元素创建的不同方式---三种方式,5个案例
元素创建-----为了提高用户的体验 元素创建的三种方式: 1. document.write("标签的代码及内容"); 2. 对象.innerHTML="标签及代码 ...
- C lang: VLA(variable-length array)
Xx_VLA Introduction VLA:variable-length array,not variable array size,but variable arary dimensional ...
- IDEA项目更改项目名
点击File,如图:
- Test111
这是一个测试 以下是截图 以下是代码标记 @@@code [XmlRpcMethod("blogger.deletePost")] haaa ggg @@# publi ...
- (绿色)修正版gooflow流程解决方案(源码分享+在线演示+UI地址下载)
gooflow出现挖矿机木马,请勿随意去其他网站下载!!! 一.功能简介 gooflow功能清单1.自定义流程绘制2.自定义属性添加3.支持3种步骤类型普通审批步骤自动决策步骤手动决策步骤 4.决策方 ...
- md2all 简单实用
. 简单使用 md2all 使用地址:http://md.aclickall.com/ . 常用操作 #代表标题等级 英文下```java(语言)``` 代表代码块
- Java之通过接口获取数据并用JDBC存储到数据库中
最近做数据同步功能,从接口获取数据然后存到数据库中以便后续对数据进行相关操作,下面就贴一下相关代码. import com.alibaba.fastjson.JSON; import com.alib ...