LeetCode OJ--Median of Two Sorted Arrays ***
http://oj.leetcode.com/problems/median-of-two-sorted-arrays/
找两个有序数组的中位数,因为有序数组,并且复杂度要求O(lg(m+n))所以想到了使用二分,但具体怎么做,没分析清楚。
参考了网上http://blog.csdn.net/zxzxy1988/article/details/8587244 ,其他的类似找两个有序数组中第 k 大的也可以这样处理。
#include <iostream>
using namespace std; double findKth(int a[],int m,int b[],int n,int k)
{
if(m>n) //总是让a中个数小于b中个数
return findKth(b,n,a,m,k);
if(m == ) //一个数组为空了,只需要在b中求第k个
return b[k-];
if(k == ) //就差一个数了
return min(a[],b[]);
int pa = min(k/,m), pb = k - pa; //好写法
if(a[pa - ]<b[pb-]) //a中的pa个可以都算进来了
return findKth(a+pa,m-pa,b,n,k-pa); //a+pa,m-pa来表示,good
else if(a[pa-]>b[pb-])
return findKth(a,m,b+pb,n-pb,k-pb);
else //如果相等
return a[pa - ];
}
class Solution {
public:
double findMedianSortedArrays(int A[], int m, int B[], int n) {
int total = m + n;
if(total & 0x1) //如果一共有奇数个
return findKth(A,m,B,n,total/+);
else //中位数 如果有偶数个数据,那么就是中间两个数字的平均数
return (findKth(A,m,B,n,total/) + findKth(A,m,B,n,total/+) )/;
}
}; int main()
{
int A[] = {,,,,};
int B[] = {,,,};
Solution *myS = new Solution();
double ans = myS->findMedianSortedArrays(A,,B,);
cout<<ans<<endl;
return ;
}
LeetCode OJ--Median of Two Sorted Arrays ***的更多相关文章
- 【算法之美】求解两个有序数组的中位数 — leetcode 4. Median of Two Sorted Arrays
一道非常经典的题目,Median of Two Sorted Arrays.(PS:leetcode 我已经做了 190 道,欢迎围观全部题解 https://github.com/hanzichi/ ...
- LeetCode(3) || Median of Two Sorted Arrays
LeetCode(3) || Median of Two Sorted Arrays 题记 之前做了3题,感觉难度一般,没想到突然来了这道比较难的,星期六花了一天的时间才做完,可见以前基础太差了. 题 ...
- 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 4. Median of Two Sorted Arrays(二分)
4. Median of Two Sorted Arrays 题目链接:https://leetcode.com/problems/median-of-two-sorted-arrays/ Descr ...
- LeetCode 4. Median of Two Sorted Arrays & 归并排序
Median of Two Sorted Arrays 搜索时间复杂度的时候,看到归并排序比较适合这个题目.中位数直接取即可,所以重点是排序. 再来看看治阶段,我们需要将两个已经有序的子序列合并成一个 ...
- 第三周 Leetcode 4. Median of Two Sorted Arrays (HARD)
4. Median of Two Sorted Arrays 给定两个有序的整数序列.求中位数,要求复杂度为对数级别. 通常的思路,我们二分搜索中位数,对某个序列里的某个数 我们可以在对数时间内通过二 ...
- Leetcode 4. Median of Two Sorted Arrays(中位数+二分答案+递归)
4. Median of Two Sorted Arrays Hard There are two sorted arrays nums1 and nums2 of size m and n resp ...
- LeetCode 004 Median of Two Sorted Arrays
题目描述:Median of Two Sorted Arrays There are two sorted arrays A and B of size m and n respectively. F ...
- [LeetCode][Python]Median of Two Sorted Arrays
# -*- coding: utf8 -*-'''https://oj.leetcode.com/problems/median-of-two-sorted-arrays/ There are two ...
- leetcode 4. Median of Two Sorted Arrays
https://leetcode.com/problems/median-of-two-sorted-arrays/ There are two sorted arrays nums1 and num ...
随机推荐
- SSH框架面试总结----1
1:struts2的工作流程 1)客户端浏览器发出HTTP请求. 2)根据web.xml配置,HTTP请求会被FilterDispatcher接收. 3)根据struts.xml,找到对应的Actio ...
- 【laravel】laravel class 里面定义以head开头的方法会报错
BadMethodCallException in Macroable.php line 81:Method head does not exist.
- linux系统装载ELF过程
参考:程序员的自我修养 fork -->execve() //----kenerl space--------------- sys_execve() /*arch\i386\kernel\pr ...
- LeetCode(234) Palindrome Linked List
题目 Given a singly linked list, determine if it is a palindrome. Follow up: Could you do it in O(n) t ...
- ACM/ICPC 2018亚洲区预选赛北京赛站网络赛 A.Saving Tang Monk II(优先队列广搜)
#include<bits/stdc++.h> using namespace std; ; ; char G[maxN][maxN]; ]; int n, m, sx, sy, ex, ...
- Centos7 install Openstack Juno (RDO) (转载)
原文地址:http://www.hdume.com/centos-7-0%E5%AE%89%E8%A3%85openstack/ 1.安装系统,Centos7镜像采用CentOS-7.0-1406-x ...
- HDU 5236 Article 期望
题意: 你现在要打\(n\)个字符,但是程序随时可能会崩溃. 你可以在恰当的时机按下 \(Ctrl-S\)键,崩溃后,会从最后一次保存的情况继续开始打字. 具体是这样的: 在每个第\(i-0.1s(i ...
- 第二章 jquery的dom操作
三个方面 dom核心,html-dom和css-dom 一. 1.dom core核心 document.getElementsByTagName("form") 获取表单 ...
- 我的PC必装软件
我的PC必装软件 TeXLive LaTeX套装 WinEdt LaTeX编辑器 PyCharm Python IDE Inkscape 矢量图制作,我根本不会用,但是常用来把SVG转成*.tex文件 ...
- 静态方法,Arrays类,二维数组
一.静态方法 静态方法属于类的,可以直接使用类名.方法名()调用. 静态方法的声明 访问修饰符 static 类型 方法名(参数列表) { //方法体 } 方法的作用:一个程序分解成几个方法,有利于快 ...