二分。情况讨论

因为数组有序,所以能够考虑用二分。通过二分剔除掉肯定不是第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. 2019-03-25 Python Pandas 基本操作

    新建表 data1 = { "name": ["Tom", "Bob", "Mary", "James&quo ...

  2. 洛谷 P1414 又是毕业季II (多个数的最大公因数)

    这道题其实不难,但是我想复杂了 我想的是把每个数质因数分解,然后每次就枚举每个质因数 来求最小公倍数. 然后想了想这样复杂度将会非常的大,肯定超时 然后看了题解发现不需要质因数分解,直接存因数的个数就 ...

  3. SpringBoot2.0中使用订阅redis的多个频道的消息

    声明:参考文章:https://blog.csdn.net/myNameIssls/article/details/75471012?locationNum=2&fps=1 一·使用maven ...

  4. excel2013超链接进不去,提示“您的组织策略不允许...”

    搜索regedit 然后找到HKEY_CURRENT_USER->Software->Classes->.html 右键修改或者双击修改数值数据为Htmlfile 关闭之后此窗口,关 ...

  5. HTML---经常使用标签总结与实践

    什么是HTML? 超文本标记语言,标准通用标记语言下的一个应用.    "超文本"就是指页面内能够包括图片.链接,甚至音乐.程序等非文字元素. 超文本标记语言的结构包含" ...

  6. [debug]重定义默认參数

    编敲代码过程中遇到重定义默认參数的错误,例如以下例所看到的: #include<iostream> #include<stdlib.h> using namespace std ...

  7. iOS UI01_UIView

    // //  AppDelegate.m //  UI01_UIView // //  Created by dllo on 15/7/29. //  Copyright (c) 2015年 zhoz ...

  8. 嵌入式外部中断控制编程方法论—比較CC2541(51核)和S5PV210(ARM核)

    这是一篇阐述怎样对嵌入式SOC外部中断进行控制编程的方法论文章.希望读者理解本篇文章后.能够具备对市场上全部已经面世和将来面世的嵌入式芯片的外部中断进行控制编程的能力. 笔者原创的技术分享一直都恪守下 ...

  9. mysql安装,数据库连接

    安装教程http://jingyan.baidu.com/article/e3c78d64412ae83c4c85f5fd.html 首先打开MySQL官网,找到Downloads标签,点击进入.如果 ...

  10. ARIMA模型实例讲解——网络流量预测可以使用啊

    ARIMA模型实例讲解:时间序列预测需要多少历史数据? from:https://www.leiphone.com/news/201704/6zgOPEjmlvMpfvaB.html   雷锋网按:本 ...