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 . ...
随机推荐
- PHP json_encode 让URL//不转义
$json_info=json_encode((object)$data,JSON_UNESCAPED_SLASHES);
- opennebula kvm 创建虚拟机错误
Thu Jul :: : Error executing image transfer script: Error copying localhost.localdomain:/app/openneb ...
- 24、Linux 多线程压缩工具pigz 的学习
转载: https://blog.csdn.net/q871761987/article/details/72230355 https://blog.csdn.net/woodcorpse/artic ...
- 函数有一个特殊的属性 prototype!
JavaScript 中只有对象,没有类. 实际上,JavaScript 才是真正应该被称为「面向对象」的语言,因为它是少有的可以不通过类,直接创建对象的语言. 函数的 prototype 属性 在 ...
- 第05章-构建Spring Web应用程序
1. Spring MVC起步 1.1 跟踪Spring MVC的请求 前端控制器DispatcherServlet 请求旅程的第一站是Spring的DispatcherServlet.与大多数基于J ...
- IOC AOP 设计模式
IOC AOP 不是什么技术而是一种设计模式 学习 IOC AOP 其实是在学习一种思想. 1.IOC IOC其实是 将对象的创建和获取提取到外部.由外部IOC容器提供需要的组件. 看下面代码: p ...
- PhoneGap Android环境搭建
原文地址:http://www.cnblogs.com/shawn-xie/archive/2012/08/15/2638480.html 一.安装 在安装PhoneGap开发环境之前,需要按顺序安装 ...
- java jeesite.properties配置文件属性提取
package com.thinkgem.jeesite.common.config; import java.io.UnsupportedEncodingException; import java ...
- c# as与is的区别
在c#语言中关于类型的判断与转换有is和as这2种操作符,具体用法如下: is检查一个对象是否兼任与指定的类型,并返回一个Boolean值:true或false,主要,is操作符永远不会抛出异常,一下 ...
- 如何彻底删除TFS上的团队项目 For VS 2017
参考 Visual Studio 2017 TFSDeleteProject.exe 位置 X:\Program Files (x86)\Microsoft Visual Studio\2017\En ...