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 ...
随机推荐
- cin 和 getline 混用中需要注意的问题
这段时间在刷题过程中遇到一个cin和getline混合使用中的问题,解决之后记录如下: 先来看一段代码 #include <iostream> #include <string> ...
- POJ-1961-Period(ZOJ-2177)
这题是最短循环节,然后我们尝试小于字符串长度的所有长度,如果符合,我们就输出. 如果它等于0,说明它不循环,因为之前并没有重复的,如果i%(i-next[i])==0说明它是循环的,然后除一下得到周期 ...
- LeetCode(228) Summary Ranges
题目 Given a sorted integer array without duplicates, return the summary of its ranges. For example, g ...
- bash实例-参数/函数/统计IP
1.写一个脚本getinterface.sh,脚本可以接受参数(i,I,a),完成以下任务: (1)使用以下形式:getinterface.sh [-i interface|-I IP|-a] ...
- BZOJ 5336: [TJOI2018]party
状压最长公共子序列的DP数组,一维最多K(15)个数,且相邻两个数的差不超过1,2^15种状态,预处理转移 #include<cstdio> #include<algorithm&g ...
- meta-data
<meta-data android:name="string" android:resource="resource specification" ...
- loj2001 「SDOI2017」树点涂色
there #include <iostream> #include <cstdio> using namespace std; int n, m, dfn[100005], ...
- hdu1787 GCD Again poj 2478 Farey Sequence 欧拉函数
hdu1787,直接求欧拉函数 #include <iostream> #include <cstdio> using namespace std; int n; int ph ...
- 令人惊叹的Npm工具包
1.http-server (简单搭建http服务器) 2.json-server (JSON服务器,快速搭建resful api接口) 3.cssnano (css多功能优化工具) PS:比uncs ...
- 聊聊、Highcharts 动态数据优化版
好久没来博客园了,最近项目太紧.上一篇写了 <Highcharts 之[动态数据]>,不够完善,虽然横纵轴可以动态取数据,但是行业信息是固定的,不能随着大数据热点改变.废话不说,直接上代码 ...