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 . ...
随机推荐
- SQLAlchemy(ORM框架)
SQLAlchemy SQLAlchemy概述 2 3 4 5 6 7 8 9 10 11 12 13 MySQL-Python mysql+mysqldb://<user>:&l ...
- Luogu 3261 [JLOI2015]城池攻占
BZOJ 4003 需要实现一个可并堆. 每个点维护一个小根堆,然后一开始把所有骑士加入到它所在的点的小根堆当中,实际上空间是$O(m)$的,然后我们从上到下不断合并这个小根堆,合并完之后如果遇到堆顶 ...
- python3--装饰器高级学习版
__author__ = "Aaron Fan"import time #导入time模块user,passwd = 'alex','abc123' #用户名密码def auth( ...
- 4、MemorySubSystem
1.概述 S3C6410X存储器子系统包括7个存储器控制器,SROM控制器,两个OneNAND控制器,一个NAND Flash控制器,一个CF控制器,一个DRAM控制器.静态存储器控制器,oneNAN ...
- bootstrap导航菜单
<!DOCTYPE html><html lang="zh-cn"><head><meta charset="utf-8&quo ...
- HandleErrorAttribute只能处理httpStatusCode为500的异常(服务器异常)
HandleErrorAttribute源代码: [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited ...
- 第20章-使用JMX管理Spring Bean
Spring对DI的支持是通过在应用中配置bean属性,这是一种非常不错的方法.不过,一旦应用已经部署并且正在运行,单独使用DI并不能帮助我们改变应用的配置.假设我们希望深入了解正在运行的应用并要在运 ...
- JAVA的编码转换测试
package test; import java.io.UnsupportedEncodingException; /** * * @author jim */ public class Test ...
- python3 tkinter 获取输入字符串长度
python 3 获取输入字符长度 #-*- coding:utf-8 -*- from tkinter import * from tkinter import messagebox def ge ...
- java并发机制的底层实现原理
volatile是轻量级的synchronized,它在多处理器开发中保证了共享变量的"可见性".可见性是说当一个线程修改一个共享变量时,另外一个线程能读到这个修改的值. vola ...