Median of Two Sorted Arrays
problem:
There are two sorted arrays nums1 and nums2 of size m and n respectively.
有两个排序好的数列num1和num大小为m和n
Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).
找到这两个数列中的中位数,其时间复杂度为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
solution:
中位数:就是有序数列中的一个数可以将数列分为长度相同的两个部分,且一个部分的所有值都大于这个数,另一部分的所有值都小于这个数,我们称这个数为中位数
既然是要找到中位数,那么正常遇到这种题,归并排序后再通过判断偶数和奇数来输出中位数就可以了,但是其时间复杂度并不符合要求
但是我们发现题目给出的两个数列是有序的,那么我们可以根据这个来进行划分,首先我们已知中位数可以将所有数列都分为长度相同的两个部分,那么我们可以假设
m+n/2为其中一半的长度,假设在num1这个数列中选取第i个数,那么对应的num2数列选取j个
其i,j满足i+j = m-i+n-j
那么j = (m+n)/2-i
当num1的第i+1个数小于num2的第j个数的时候说明i应该向右侧移动
当num2的第j+1个数小于num1的第i个数的时候说明i应该向左侧移动
于此同时所谓的移动其实是用的二分法,对num1进行二分,寻找i的最合适的位置
最合适也就是,只需要满足num1的第i+1个数大于num2的第j个,和num2的第j+1个大于num1的第i个
如果n+m为奇数,说明中位数为max(num1的第i个,num2第j个)
如果n+m为偶数,说明中位数为(max(num1的第i个,num2第j个)+min(num1的第i+1个,num2第j+1个))/2
代码如下:
1 class Solution(object): 2 def findMedianSortedArrays(self, nums1, nums2): 3 """ 4 :type nums1: List[int] 5 :type nums2: List[int] 6 :rtype: float 7 """ 8 9 m = len(nums1) 10 n = len(nums2) 11 if(m>n): 12 nums1,nums2,m,n = nums2,nums1,n,m 13 imin = 0 14 imax = m 15 halflen = (n+m+1)/2 16 while(imin <= imax): 17 i = (imin+imax)/2 18 j = halflen - i 19 if(i<imax and nums1[i]<nums2[j-1]): 20 imin = i+1 21 elif(i>imin and nums2[j]<nums1[i-1]): 22 imax = i-1 23 else: 24 maxleft = 0 25 if(i==0): 26 maxleft = nums2[j-1] 27 elif(j==0): 28 maxleft = nums1[i-1] 29 else: 30 maxleft = max(nums1[i-1],nums2[j-1]) 31 if((n+m)%2==1): 32 return maxleft 33 minright = 0 34 if(i == m): 35 minright = nums2[j] 36 elif(j == n): 37 minright = nums1[i] 38 else: 39 minright = min(nums1[i],nums2[j]) 40 41 return (minright+maxleft)/2.0
转载于:https://www.cnblogs.com/fzy0331-leetcodestudy/p/8406562.html
Median of Two Sorted Arrays相关推荐
- 【LeetCode】004 Median of Two Sorted Arrays 两个排序数组合并后的中位数
题目:LeetCode 004 Median of Two Sorted Arrays There are two sorted arrays nums1 and nums2 of size m an ...
- LeetCode: Median of Two Sorted Arrays 解题报告
Median of Two Sorted Arrays There are two sorted arrays A and B of size m and n respectively. Find t ...
- Kotlin实现LeetCode算法题之Median of Two Sorted Arrays
题目Median of Two Sorted Arrays(难度Hard) 方案1,数组合并&排序调用Java方法 1 import java.util.* 2 3 class Solutio ...
- LeetCode刷题第二天——3Longest Substring Without repeating character 4 Median of Two Sorted Arrays...
混淆点: 子串 连续 子序列 可以不连续 知识点: HashMap: 出现问题: 1.使用unordered_map头文件时报错 #error This file requires compiler ...
- 算法—两个有序数组的中位数 Median of Two Sorted Arrays
关注微信公众号:CodingTechWork,一起学习进步. 题目 There are two sorted arrays nums1 and nums2 of size m and n respec ...
- Leetcode平台上的Median of Two Sorted Arrays题目用Java快排实现
Leetcode平台上的Median of Two Sorted Arrays题目,大意就是找两个已排序数组的中位数.今天先用快排的方式实现一下,代码如下: There are two sorted ...
- LeetCode Median of Two Sorted Arrays (DFS)
There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted ...
- 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 th ...
- Leet Code OJ 4. Median of Two Sorted Arrays [Difficulty: Hard]
题目 There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the ...
最新文章
- TIOBE Programming Community Index
- Microsoft 和 Google 就Yahoo 收购一事展开口水战
- 浅谈BroadcastReceiver
- 代理类和被代理类的解释及案例
- C++11 unique_ptr用法
- 《 追风筝的人 》:“ 为你,千千万万遍 ” ...
- 多媒体个人计算机软件系统,HP推出新的多媒体个人计算机
- 金蝶osf接口开发_解决SaaS间的数据孤岛,实现SaaS数据接口集成互通互联
- 型管件的作用_W型柔性铸铁排水管适用范围
- jQuery Ajax Demo
- 纯福利文章、送5本Java核心技术(不定期送福利)
- python机械臂写字_SCARA机器人 机械手臂 写字机 DIY 视觉识别
- 基于Nextcloud,挂载Google drive,搭建Aria2+AriaNg,实现在线下载BT磁链/在线观看/全功能文件管理/无限容量云盘
- 2022年虾皮开店(shopee入驻)最新教程!
- UITableViewCell中嵌套UITableView,用UITextView加载HTML数据
- 传统图像增强算法python实现
- WIFI驱动配置实战(Linux驱动开发篇)
- LevelDB 源码分析
- 序文 【IT圈是个什么玩意儿 1 】
- webview 禁止苹果自动下拉_苹果手机如何拥有百变铃声?酷狗铃声1分钟搞定!-时尚呼吸...
热门文章
- java学习笔记:使用dom4j解析xml
- 类linux系统/proc/sysrq-trigger文件功能作用
- EasyUI 1.3.6 行号显示不全
- 我去,JS自执行匿名函数竟然有20几种写法!
- yii框架相关知识(转)
- Hadoop平台作业参数设置关于mapreduce.job.split.metainfo.maxsize的说明
- Java键盘字符乱码判断代码
- Pandas简明教程:七、Pandas缺失数据的处理(数据清洗基础)
- 什么是async、await?
- CTFshow 命令执行 web47