3.Median of Two Sorted Arrays Leetcode
难度系数:5星
/***************************************************************************
题目:有两个排好序的数组,要求其中位数,时间复杂度控制在O(log(m+n))
一般化:求两个排序数组的第K大的元素
直观思路1:PA和PB首先指向这两个子数组,对比比较,A小则PA++,m++,B小则
PB++,m++,直至m==k ======>> 时间复杂度O(m+n)
正解思路:
由于数组是有序的,又是有序的,能否通过二分等思路来可能筛掉一些元素
关键是设计一种方案让其收敛到最终位置
尝试二分: 若Amid<Bmid,cutA前一半,B后一半,
能确定的是A前一半小于A后一半以及B后一半。
A------------cut---------------
B------cut--------
那么A的前一半不可能是中位数,筛掉,要是Bmid小呢,只能筛掉B的前一半
这样不稳定
如果以K的一半为标准呢:
A[k/2-1]==B[k/2-1],说明AB的前k/2-1+k/2-1=k-2个元素在topK内
那么 A[k/2-1]便是第K-1和第K大的数(K为偶数)
A[K/2-1]>B[k/2-1] 去除掉A数组前K/2元素(实际最大能去除整个数组元素)
A[K/2-1]<B[k/2-1]
step1:当A或B是空时,直接返回A[K-1]或者B[k-1]
step2:当K=1,返回min[A[0],B[0]]
step3:当A[K/2-1]==B[k/2-1] 返回A[K/2-1]
************************************************************/
class Solution{
public:
double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2){
int m = nums1.size(),n = nums2.size();
int total = m+n;
cout<<total<<endl;
if(total&0x1)
return find_k(&nums1[0],m,&nums2[0],n,total/2+1);
else
{
return (find_k(&nums1[0],m,&nums2[0],n,total/2)
+find_k(&nums1[0],m,&nums2[0],n,total/2+1))/2.0;
}
}
private:
static int find_k(int A[],int m,int B[],int n,int k)
{
if(m>n)return find_k(B,n,A,m,k);
if(m==0) return B[k-1];
if(k==1)return min(A[0],B[0]);
int ia = min(m,k/2),ib = k-ia;
if(A[ia-1]<B[ib-1]) return find_k(A+ia,m-ia,B,n,ib);
else if(A[ia-1]>B[ib-1])
return find_k(A,m,B+ib,n-ib,k-ib);
else return A[ia-1];
}
};
int main()
{
int a[]={1,3,5,7,9};
int b[]={2,4,6};
Solution S;
vector<int> B(b,b+3),A(a,a+5);
cout<<S.findMedianSortedArrays(A,B)<<endl;
return 0;
}
3.Median of Two Sorted Arrays Leetcode的更多相关文章
- Median of Two Sorted Arrays LeetCode Java
两排序好的数组,找中位数 描述There are two sorted arrays A and B of size m and n respectively. Find the median of ...
- 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 ...
- 【算法之美】求解两个有序数组的中位数 — leetcode 4. Median of Two Sorted Arrays
一道非常经典的题目,Median of Two Sorted Arrays.(PS:leetcode 我已经做了 190 道,欢迎围观全部题解 https://github.com/hanzichi/ ...
- LeetCode 4 Median of Two Sorted Arrays (两个数组的mid值)
题目来源:https://leetcode.com/problems/median-of-two-sorted-arrays/ There are two sorted arrays nums1 an ...
- LeetCode(3) || Median of Two Sorted Arrays
LeetCode(3) || Median of Two Sorted Arrays 题记 之前做了3题,感觉难度一般,没想到突然来了这道比较难的,星期六花了一天的时间才做完,可见以前基础太差了. 题 ...
- Kotlin实现LeetCode算法题之Median of Two Sorted Arrays
题目Median of Two Sorted Arrays(难度Hard) 方案1,数组合并&排序调用Java方法 import java.util.* class Solution { fu ...
- Leetcode 4. Median of Two Sorted Arrays(二分)
4. Median of Two Sorted Arrays 题目链接:https://leetcode.com/problems/median-of-two-sorted-arrays/ Descr ...
- leetcode第四题:Median of Two Sorted Arrays (java)
Median of Two Sorted Arrays There are two sorted arrays A and B of size m and n respectively. Find t ...
- LeetCode 4. Median of Two Sorted Arrays & 归并排序
Median of Two Sorted Arrays 搜索时间复杂度的时候,看到归并排序比较适合这个题目.中位数直接取即可,所以重点是排序. 再来看看治阶段,我们需要将两个已经有序的子序列合并成一个 ...
随机推荐
- Spring源码分析-BeanFactoryPostProcessor
Spring源码分析-BeanFactoryPostProcessor 博主技术有限,本文难免有错误的地方,如果您发现了欢迎评论私信指出,谢谢 BeanFactoryPostProcessor接口是S ...
- python3.5 安装mysqlclient
python 3.5 安装 mysqlclient 会失败 pip install mysqlclient 注意这里环境中只有python3.5 会出现一大堆红字 编译终止, error: comma ...
- 痞子衡嵌入式:借助Serial Plot软件测量i.MXRT系列FlexSPI驱动Flash页编程执行时间
大家好,我是痞子衡,是正经搞技术的痞子.今天痞子衡给大家介绍的是i.MXRT系列FlexSPI驱动Flash页编程执行时间. 痞子衡之前写过一篇文章 <串行NOR Flash的页编程模式对于量产 ...
- Go语言核心36讲(Go语言进阶技术十三)--学习笔记
19 | 错误处理(上) 提到 Go 语言中的错误处理,我们其实已经在前面接触过几次了. 比如,我们声明过error类型的变量err,也调用过errors包中的New函数. 我们说过error类型其实 ...
- 测试平台系列(71) Python定时任务方案
大家好~我是米洛! 我正在从0到1打造一个开源的接口测试平台, 也在编写一套与之对应的完整教程,希望大家多多支持. 欢迎关注我的公众号测试开发坑货,获取最新文章教程! 定时任务 定时任务,顾名思义: ...
- Java-基础-HashMap
1. 简介 Java8 HashMap结构(数组 + 列表 + 红黑树)如图: 基于哈希表的 Map 接口的实现.此实现提供所有可选的映射操作,并允许使用 null 值和 null 键.(除了非同步和 ...
- Centos6.8 yum报错及修复YumRepo Error: All mirror URLs are not using ftp, http[s] or file. Eg. Invalid
问题 使用yum安装软件时报错 YumRepo Error: All mirror URLs are not using ftp, http[s] or file. Eg. Invalid relea ...
- fabric运行记录
创建第一个fabric网络 Generate Network Artifacts cd first-network 先关闭服务 ./byfn.sh -m down 然后创建 ./byfn.sh -m ...
- idea离线安装lombok插件
1.查看自己idea版本,2019.1.2,必须安装相同版本的插件 2.从http://plugins.jetbrains.com/plugin/6317-lombok-plugin中下载对应版本的l ...
- 大一C语言学习笔记(11)---编程篇--写一个程序,可以获取从键盘上输入的的三个数,并能够判断是否可以以这三个数字作为边长来构成一个三角形,如果可以的话,输出此三角形的周长及面积,要求 0 bug;
考核内容: 写一个程序,可以获取从键盘上输入的的三个数,并能够判断是否可以以这三个数字作为边长来构成一个三角形,如果可以的话,输出此三角形的周长及面积: 答案: #include<stdio.h ...