LeetCode4. 两个排序数组的中位数
4. 两个排序数组的中位数
问题描述
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
思路
- 直接用python自带的函数,将两个列表相加,生成一个新列表。对新列表进行排序,然后求中位数。
class Solution:
def findMedianSortedArrays(self, nums1, nums2):
"""
:type nums1: List[int]
:type nums2: List[int]
:rtype: float
"""
resultList = nums1 + nums2
resultList.sort()
numLength = len(resultList)
if numLength % 2 == 1:
index = int((numLength + 1) / 2) - 1
return resultList[index]
else:
index = int(numLength / 2) - 1
return (resultList[index] + resultList[index + 1]) / 2
- 由于两个列表自身有序,循环两个列表,将值较小的放入新列表中,把剩下的整个列表放到新列表中。则新列表有序,求中位数。
class Solution:
def findMedianSortedArrays0(self, nums1, nums2):
"""
:type nums1: List[int]
:type nums2: List[int]
:rtype: float
"""
nums3 = [0] * (len(nums1) + len(nums2))
r_i, l_i, i = 0, 0, 0
while (l_i < len(nums1)) and (r_i < len(nums2)):
if nums1[l_i] < nums2[r_i]:
nums3[i] = nums1[l_i]
l_i = l_i + 1
else:
nums3[i] = nums2[r_i]
r_i = r_i + 1
i += 1
if l_i != len(nums1):
nums3[i:] = nums1[l_i:]
else:
nums3[i:] = nums2[r_i:]
len_3 = len(nums3)
if len_3 % 2 != 0:
return float(nums3[(len_3 - 1) // 2])
return (nums3[(len_3 - 1) // 2] + nums3[len_3 // 2]) / 2
结果思考
方法1要优于方法2,我认为可能是python提供的 sort方法的时间复杂度较低。
GitHub地址:https://github.com/protea-ban/LeetCode

LeetCode4. 两个排序数组的中位数的更多相关文章
- LeetCode-4. 两个排序数组的中位数(详解)
链接:https://leetcode-cn.com/problems/median-of-two-sorted-arrays/description/ 有两个大小为 m 和 n 的排序数组 nums ...
- [Swift]LeetCode4. 两个排序数组的中位数 | 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 ...
- 2.Median of Two Sorted Arrays (两个排序数组的中位数)
要求:Median of Two Sorted Arrays (求两个排序数组的中位数) 分析:1. 两个数组含有的数字总数为偶数或奇数两种情况.2. 有数组可能为空. 解决方法: 1.排序法 时间复 ...
- JavaScript实现获取两个排序数组的中位数算法示例
本文实例讲述了JavaScript排序代码实现获取两个排序数组的中位数算法.分享给大家供大家参考,具体如下: 题目 给定两个大小为 m 和 n 的有序数组 nums1 和 nums2 . 请找出这两个 ...
- LeetCode(4):两个排序数组的中位数
Hard! 题目描述: 有两个大小为 m 和 n 的排序数组 nums1 和 nums2 . 请找出两个排序数组的中位数并且总的运行时间复杂度为 O(log (m+n)) . 示例 1: nums1 ...
- Leetcode4--->求两个排序数组的中位数
题目:给定两个排序数组,求两个排序数组的中位数,要求时间复杂度为O(log(m+n)) 举例: Example 1: nums1 = [1, 3] nums2 = [2] The median is ...
- leetcode4:两个排序数组的中位数
给定两个大小为 m 和 n 的有序数组 nums1 和 nums2 . 请找出这两个有序数组的中位数.要求算法的时间复杂度为 O(log (m+n)) . 1.我的思路:直接用sort,时间复杂度应如 ...
- Leetcode4.Median of Two Sorted Arrays两个排序数组的中位数
给定两个大小为 m 和 n 的有序数组 nums1 和 nums2 . 请找出这两个有序数组的中位数.要求算法的时间复杂度为 O(log (m+n)) . 你可以假设 nums1 和 nums2 不同 ...
- 从0打卡leetcode之day 5 ---两个排序数组的中位数
前言 我靠,才坚持了四天,就差点不想坚持了.不行啊,我得把leetcode上的题给刷完,不然怕是不好进入bat的大门. 题目描述 给定两个大小为 m 和 n 的有序数组 nums1 和 nums2 . ...
随机推荐
- 【总结整理】pv、uv
1.pv的全称是page view,译为页面浏览量或点击量,通常是衡量一个网站甚至一条网络新闻的指标.用户每次对网站中的一个页面的请求或访问均被记录1个PV,用户对同一页面的多次访问,pv累计.例如, ...
- jsoup 的简单应用
导入相关jar包 package jsoup.zr.com.utils; import java.io.IOException; import java.util.List; import org.j ...
- 浅析junit4及扩展实践
junit框架相关源代码分析,网上已经有很多了,本篇不做过多相关解说,主要还是要自己多读相关源代码.本篇主要对自动化测试过程相关的测试用例,测试数据,测试结果结合junit做相关扩展说明. 如果要解读 ...
- linux的deamon后台运行
有的时候需要将程序一直跑在后台,比如一些服务类代码,或者一些监控类代码.使用deamon是正确的一种思路. 以前我们在看<unix环境高级编程>的时候,有专门的整章详细介绍如何编写一个后台 ...
- javac老提示无效的标记
加上-cp libs/*后,就开始提示无效的标记,搞了半天,似乎是shell展开的问题,估计是把后面的jar文件当源文件了? 加上引号就行了-cp "libs/*",不让shell ...
- Luogu 3265 [JLOI2015]装备购买
BZOJ 4004 把所有不能相互表示出来的向量都买下,一定能得到最大能买的方案数. 求解线性无关向量可以高斯消元,最后没有变成$0$向量的就是基底. 本题还要求代价最小怎么办?我们只要先把所有向量按 ...
- Luogu 3066 [USACO12DEC]逃跑的BarnRunning Away From…
好像是某CF的题,不记得…… 很套路的题,但是觉得可以做一下笔记. 倍增 + 差分. 有一个比较简单的思路就是每一个点$x$向上走一走,直到走到一个点$y$使总路程恰好不超过超过了$L$,然后把$(x ...
- ofbiz
http://www.cnblogs.com/Ivan-j2ee/category/404613.html 本类别主要收集一些关于ofbiz的技术文档,包括一些原创文档
- poj 1988 Cube Stacking (并查集)
题意:有N(N<=30,000)堆方块,开始每堆都是一个方块.方块编号1 – N. 有两种操作: M x y : 表示把方块x所在的堆,拿起来叠放到y所在的堆上. C x : 问方块x下面有多少 ...
- OpenGL绘图框架(GLFW)
下载地址:http://www.glfw.org/download.html