LeetCode: Median of Two Sorted Arrays
来源:互联网 发布:安卓沙箱软件 编辑:程序博客网 时间:2024/06/11 08:20
查找第k大的数
class Solution {
public:double findKthElem(int A[],int m,int B[],int n,int k){
int alow,amid,ahi,blow,bmid,bhi;
alow = 0,ahi = m-1,blow = 0,bhi = n-1;
while(alow<=ahi&&blow<=bhi){
amid = alow+(ahi-alow)/2;
bmid = blow+(bhi-blow)/2;
if(A[amid]<B[bmid]){
if(k<=amid-alow+1+bmid-blow)
bhi = bmid-1;
else{
k -= amid-alow+1; //k先减
alow = amid+1;
}
}
else{
if(k<=amid-alow+1+bmid-blow)
ahi = amid-1;
else{
k -= bmid-blow+1;
blow = bmid+1;
}
}
}
if(alow>ahi){
return B[blow+k-1];
}
if(blow>bhi)
return A[alow+k-1];
}
double findMedianSortedArrays(int A[], int m, int B[], int n) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if((m+n)%2==1)
return findKthElem(A,m,B,n,(m+n+1)/2);
else
return (findKthElem(A,m,B,n,(m+n)/2)+findKthElem(A,m,B,n,(m+n)/2+1))/2;
}
};
- LeetCode: Median of Two Sorted Arrays
- LeetCode Median of Two Sorted Arrays
- leetcode 26: Median of Two Sorted Arrays
- Leetcode 4 Median of Two Sorted Arrays
- [Leetcode] Median of Two Sorted Arrays
- LeetCode 4 - Median of Two Sorted Arrays
- [LeetCode]Median of Two Sorted Arrays
- leetcode Median of Two Sorted Arrays
- LeetCode-Median of Two Sorted Arrays
- [LeetCode] Median of Two Sorted Arrays
- [LeetCode] Median of Two Sorted Arrays
- [leetcode] Median of Two Sorted Arrays
- leetcode-004:Median of Two Sorted Arrays
- Leetcode 4 Median of Two Sorted Arrays
- leetcode之 median of two sorted arrays
- 【转载】【leetcode】Median of Two Sorted Arrays
- LeetCode - Median of Two Sorted Arrays
- [LeetCode]Median of Two Sorted Arrays
- java.lang.VerifyError: Expecting a stackmap frame at branch target 12 in method
- linux内核-内存寻址
- HDOJ 4762 - Cut the Cake 推公式.高精度
- 手机通过笔记本上网
- Linux常用命令
- LeetCode: Median of Two Sorted Arrays
- Eclipse Android SDK无法下载问题不翻墙解决办法
- java笔试题---程序产生一个int数组,长度为100,并向其中随机插入1-100,并且不能重复。
- ZOJ Problem Set - 1091
- PiscisOS开发笔记_1_PiscisOS的诞生和系统特性简介
- nor flash 驱动
- Tomcat Neither the JAVA_HOME nor the JRE_HOME environment variable is defined
- vim Visual Mode
- PiscisOS开发笔记_2_开发方法详解