median-of-two-sorted-arrays
排序的代价过于高,我们可以采用挪动指针的方法找到第k大的值
There are two sorted arrays A and B 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)).
参考了大神的代码:http://blog.csdn.net/zxzxy1988/article/details/8587244
#include <iostream>
#include <map>
#include <string>
using namespace std;class Solution {
public:double findMedianSortedArrays(int A[], int m,int B[], int n) {int k = (m+n)%2;if (k==0) return (f(A,m,B,n,(m+n)/2)+f(A,m,B,n,(m+n)/2+1))/2;else return f(A,m,B,n,(m+n)/2+1);}
};double f(int a[],int m,int b[],int n,int k) {if(m>n) return f(b,n,a,m,k);if(m==0) return b[k-1];if(k==1) return min(a[0],b[0]);int pa = min(k/2,m),pb = k-pa;if(a[pa-1]<b[pb-1]) {return f(a+pa,m-pa,b,n,k-pa);}else if(b[pb-1]<a[pa-1]) {return f(a,m,b+pb,n-pb,k-pb);}else return b[pb-1];
}
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 ...
- Median of Two Sorted Arrays
problem: There are two sorted arrays nums1 and nums2 of size m and n respectively. 有两个排序好的数列num1和num ...
- 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 ...
最新文章
- IDEA界面太丑??尝试一下这几个插件!
- 【双100%解法】LeetCode 141 【剑指Offer 23】链表中环的入口节点
- python读取字典数据_Python:读取列表[{}]中的字典数据
- Fread 和fwrite的参数不同,返回值不同
- 听说做开发的工资随随便便就上10k?
- 【boost】time.1 同步计数器
- Maven项目添加FindBugs、PMD插件
- 10.傅里叶变换——2D中的傅里叶变换,傅里叶变换的应用_5
- Cisco服务器怎么安装系统,CISCO服务器Linux系统安装步骤
- 尽显中国风 | 高品质海报背景,PSD分层,智能替换展示商品
- 软件_搭建rtsp视频推送环境
- asp.net的10个提升性能或扩展性的秘密(二)
- 百度文库免积分下载器
- K8S-Cluster部署
- 微擎使用腾讯地图拾取坐标
- 黑色背景视频和白色背景图片变透明的方法
- linux wifi信道,2.4G wifi 的频道/信道 20M 40M的概念,区别
- ajax 的四步法处理
- 资深研发转变为架构师意味着什么
- 访达前往文件夹_(苹果电脑excle没有查找全部)苹果笔记本有没有自带excel
热门文章
- 别再抱怨缺乏算法实践场景,不妨来挑战这场百万奖池的比赛!
- centos7mimi mysql 安装_CentOS7 64位下MySQL5.7安装与配置(YUM)
- MyBatis批量插入为什么比单条插入块?
- Django多表实例学习笔记
- Visual Studio 2022即将发布,重磅升级为64位应用程序!
- 信息系统项目管理师必背核心考点(六十一)项目组合概念
- 秋无痕 Win2003 SP2 STD ENT 2IN1 集成安装版 v2008.1
- Unity3D游戏开发之伤害数值显示
- Ubuntu 16.04 Lenovo B470 解决风扇狂转问题
- 女孩应该偷偷知道一些事情