二分。情况讨论

因为数组有序,所以能够考虑用二分。通过二分剔除掉肯定不是第k位数的区间。如果数组A和B当前处理的下标各自是mid1和mid2。则

1、假设A[mid1]<B[mid2],

①、若mid1+mid2+2==k(+2是由于下标是从0開始的),则

mid1在大有序数组中下标肯定小于k,所以能够排除[0,mid1]。此外。B[mid2]下标大于或等于k。能够排除[mid2+1,n];

②、若mid1+mid2+2<k,则

mid1在大有序数组中下标肯定小于k,所以能够排除[0,mid1]

③、若mid1+mid2+2>k,则

B[mid2]下标大于k,能够排除[mid2,n];

2、假设A[mid1]<B[mid2]情况相符,仅仅是下标改变。

这些操作处理完后。可能一个数组被排除了,即满足lowX>highX。此时仅仅需对还有一个数组进行二分,同一时候二分其元素在还有一个数组中的下标,确定全局下标,终于通过推断全局下标与k的关系。确定是否为第k数

class Solution {
public:
int findPos(int* p,int n,int x){
int low=0,high=n-1,mid;
while(low<=high){
mid=(low+high)>>1;
if(p[mid]<=x)low=mid+1;
else high=mid-1;
}
return low;
}
double findK(int a[], int m, int b[], int n,int k){
int mid1,mid2,low1=0,low2=0,high1=m-1,high2=n-1,x;
while(low1<=high1&&low2<=high2){
mid1=(high1+low1)>>1;
mid2=(high2+low2)>>1;
if(a[mid1]<b[mid2]){
if(mid1+mid2+2==k){
low1=mid1+1;
high2=mid2;
}
else if(mid1+mid2+2<k){
low1=mid1+1;
}
else high2=mid2-1;
}
else{
if(mid1+mid2+2==k){
low2=mid2+1;
high1=mid1;
}
else if(mid1+mid2+2<k){
low2=mid2+1;
}
else high1=mid1-1;
}
}
if(low1<=high1){
// if(low1==high1)return a[low1];
while(low1<=high1){
mid1=(low1+high1)>>1;
x=findPos(b,n,a[mid1]);
if(x+mid1+1==k)return a[mid1];
else if(x+mid1<k)low1=mid1+1;
else high1=mid1-1;
}
return low1>=m?a[m-1]:a[low1];
}
else {
// if(low2==high2)return b[low2];
while(low2<=high2){
mid2=(low2+high2)>>1;
x=findPos(a,m,b[mid2]);
if(x+mid2+1==k)return b[mid2];
else if(x+mid2<k)low2=mid2+1;
else high2=mid2-1;
}
return low2>=n? a[n-1]:b[low2];
}
}
double findMedianSortedArrays(int a[], int m, int b[], int n) {
int k=m+n;
if(k&1){
return findK(a,m,b,n,k/2+1);
}
else{
return (findK(a,m,b,n,k/2)+findK(a,m,b,n,k/2+1))/2.0;
}
}
};

[LeetCode]Median of Two Sorted Arrays 二分查找两个有序数组的第k数(中位数)的更多相关文章

  1. 4. Median of Two Sorted Arrays *HARD* -- 查找两个排序数组的中位数(寻找两个排序数组中第k大的数)

    There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...

  2. Leetcode 4 Median of Two Sorted Arrays 二分查找(二分答案+二分下标)

    貌似是去年阿里巴巴c++的笔试题,没有什么创新直接照搬的... 题意就是找出两个排序数组的中间数,其实就是找出两个排序数组的第k个数. 二分答案,先二分出一个数,再用二分算出这个数在两个排序数组排序第 ...

  3. Coursera Algorithms week3 快速排序 练习测验: Selection in two sorted arrays(从两个有序数组中寻找第K大元素)

    题目原文 Selection in two sorted arrays. Given two sorted arrays a[] and b[], of sizes n1 and n2, respec ...

  4. 查找两个有序数组中的第K个元素(find kth smallest element in 2 sorted arrays)

    查找两个有序数组中的第K个元素 int FindKth(int a[], int b[], int k, int astart, int aend, int bstart, int bend) { ; ...

  5. Leetcode#88. Merge Sorted Array(合并两个有序数组)

    题目描述 给定两个有序整数数组 nums1 和 nums2,将 nums2 合并到 nums1 中,使得 num1 成为一个有序数组. 说明: 初始化 nums1 和 nums2 的元素数量分别为 m ...

  6. [LeetCode] Median of Two Sorted Arrays 两个有序数组的中位数

    There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...

  7. Leetcode Median of Two Sorted Arrays

    There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted ...

  8. 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 ...

  9. [leetcode]Median of Two Sorted Arrays @ Python

    原题地址:https://oj.leetcode.com/problems/median-of-two-sorted-arrays/ 题意:There are two sorted arrays A ...

随机推荐

  1. Windows桌面美化

    [工具链接]链接: https://pan.baidu.com/s/12aUGsu91F8WfaW5HU5ps3g 提取码: dnan [样例] [美化步骤] 1.解压下载文件,安装两个软件: Sta ...

  2. 紫书 习题7-13 UVa 817(dfs+栈求表达式的值)

    题目链接  点击打开链接 这道题分为两个部分, 一用搜索枚举每种可能, 二计算表达式的值, 有挺多细节需要注意 特别注意我的代码中在计算表达式的值中用到了一个!(代码枚举中的!表示不加符号, 我现在说 ...

  3. WampServer更改或重置数据库密码

    WampServer安装后密码是空的, 修改一般有两种方式: 一是通过phpMyAdmin直接修改: 二是使用WAMP的MySql控制台修改. 第一种: ①在phpMyAdmin界面中点击[用户],将 ...

  4. php获取时间是星期几

    PHP星期几获取代码: date("l"); //data就可以获取英文的星期比如Sundaydate("w"); //这个可以获取数字星期比如123,注意0是 ...

  5. Spring Security中的MD5盐值加密

    在 spring Security 文档中有这么一句话: "盐值的原理非常简单,就是先把密码和盐值指定的内容合并在一起,再使用md5对合并后的内容进行演算,这样一来,就算密码是一个很常见的字 ...

  6. 【codeforces 810A】Straight «A»

    [题目链接]:http://codeforces.com/contest/810/problem/A [题意] 有n门课的成绩,和一个整数k代表每门课的满分都是k分; 然后这n门课的成绩是按照平均分算 ...

  7. tomcat使用及原理

    1,Tomcat作为Servlet容器的基本功能 2,Tomcat的组成结构 Tomcat本身由一列的可配置的组件构成,其中核心组件是Servlet容器组件,它是所有其他Tomcat组件的顶层容器.T ...

  8. 什么叫openapi

    Open API即开放API,也称开放平台. 所谓的开放API(OpenAPI)是服务型网站常见的一种应用,网站的服务商将自己的网站服务封装成一系列API(Application Programmin ...

  9. [React] Unit test a React Render Prop component

    In this lesson, I use Enzyme and Jest to unit test a Counter Render Prop component. Writing integrat ...

  10. Tachyon在Spark中的作用(Tachyon: Reliable, Memory Speed Storage for Cluster Computing Frameworks 论文阅读翻译)

    摘要:         Tachyon是一种分布式文件系统,能够借助集群计算框架使得数据以内存的速度进行共享.当今的缓存技术优化了read过程,可是,write过程由于须要容错机制,就须要通过网络或者 ...