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

二分。情况讨论

因为数组有序,所以能够考虑用二分。通过二分剔除掉肯定不是第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;
		}
    }
};
时间: 2024-10-06 00:34:17

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

Leetcode 4 Median of Two Sorted Arrays 二分查找(二分答案)

貌似是去年阿里巴巴c++的笔试题,没有什么创新直接照搬的... 题意就是找出两个排序数组的中间数,其实就是找出两个排序数组的第k个数. 二分答案,先二分出一个数,再用二分算出这个数在两个排序数组排序第几,然后和k做比较,最后以这个比较为依据接着二分直到求出第k个数. 本来这是两重二分,由于楼主的懒已经没有办法治疗了,用库函数upper_bound()代替了第二重二分,有志者可以自己写第二重二分,楼主就偷懒了... 警告:由于题目很神奇,会出现两个排序数组为空的情况,楼主差点因为这个报警....

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, respectively, design an algorithm to find the kth largest key. The order  of growth of the worst case running time of your algorithm should be logn, where n

[leetcode]Median of Two Sorted Arrays @ Python

原题地址:https://oj.leetcode.com/problems/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 arrays. The overall run time complexity should be O(log (m+n)). 解题思路:这道题要求两个已经排好

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 arrays. The overall run time complexity should be O(log (m+n)). 有两个已排序的数组A和B,大小为m 和 n. 找出两数组的中位数 时间复杂度应该为 O(log (m+n)). 简单粗暴的方法就是把两个数组合并成一个数组,排序,取中位数.

[LeetCode] Median of Two Sorted Arrays [16]

题目 There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)). 原题链接(点我) 解题思路 返回两个排序数组的中位数.这个题可以有以下几个思路: 首先可以想到的是将两个数组merge起来,然后返回其中位数. 第二个是,类似merg

查找两个有序数组的公共元素

1 /*查找两个有序数组的公共元素*/ 2 #include<stdio.h> 3 #include<stdlib.h> 4 5 void findCommon1(int *arr1, int *arr2, int len1, int len2); 6 void findCommon2(int *arr1, int *arr2, int len1, int len2); 7 8 9 10 int main() 11 { 12 int arr1[10] = {1,2,3,4,5,6,

leetcode | Median of Two Sorted Arrays 寻找2个有序数组中第k大的值

问题 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 arrays. The overall run time complexity should be O(log(m + n)). 分析 本题更经典通用的描述方式时: 给定2个有序数组,找出2个数组中所有元素中第k大的元素. 思路1 直观思

[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 sorted arrays. The overall run time complexity should be O(log (m+n)). Subscribe to see which companies asked this question Show Tags 分析: 这个题目在leetco

LeetCode—Median of Two Sorted Arrays

Median of Two Sorted Arrays 这道题要找Median,中位数.这个是指,如果数组大小是偶数,返回中间两个数的平均值,如果是奇数个,就是中间的数. 算法时间效率要求是 O(log(m + n)),具体思路网上都一样. 另外,现在leetCode的C++ 数组都换成vector了,所以只好整理一下vector的用法. 参考:http://imatlab.lofter.com/post/286ffc_a81276 http://www.cnblogs.com/wang7/ar