貌似是去年阿里巴巴c++的笔试题,没有什么创新直接照搬的。。。

题意就是找出两个排序数组的中间数,其实就是找出两个排序数组的第k个数。

二分答案,先二分出一个数,再用二分算出这个数在两个排序数组排序第几,然后和k做比较,最后以这个比较为依据接着二分直到求出第k个数。

本来这是两重二分,由于楼主的懒已经没有办法治疗了,用库函数upper_bound()代替了第二重二分,有志者可以自己写第二重二分,楼主就偷懒了。。。

警告:由于题目很神奇,会出现两个排序数组为空的情况,楼主差点因为这个报警。。。。

 class Solution {
public:
int findkth(vector<int>& nums1, vector<int>& nums2, int k){
int l = min(nums1[],nums2[]);
int r = max(nums1[nums1.size() - ], nums2[nums2.size() - ]);
while(l <= r){
int mid = l + (r - l)/ ;
int k1 = upper_bound(nums1.begin(), nums1.end(), mid) - nums1.begin() ;
int k2 = upper_bound(nums2.begin(), nums2.end(), mid) - nums2.begin() ;
if(k1 + k2 == k){
if(k1 > && mid == nums1[k1 - ]) return mid;
else if(k2 > && mid == nums1[k2 - ]) return mid;
else r = mid - ;
}
else if(k1 + k2 > k){
r = mid - ;
}
else l = mid + ;
}
return l;
}
double getmid(vector<int>& nums){
if(nums.size() % == ) return (nums[nums.size() / - ] + nums[nums.size() / ]) / 2.0;
else return nums[nums.size() / ];
}
double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
if(nums1.size() == && nums2.size() == ) return 0.0;
if(nums1.size() == ) return getmid(nums2);
if(nums2.size() == ) return getmid(nums1);
int k = (nums1.size() + nums2.size() + ) / ;
if((nums1.size() + nums2.size()) % == ){
return (findkth(nums1, nums2, k) + findkth(nums1, nums2, k + ))/2.0;
}
else return findkth(nums1, nums2, k);
}
};

Leetcode 4 Median of Two Sorted Arrays 二分查找(二分答案+二分下标)的更多相关文章

  1. 【算法之美】求解两个有序数组的中位数 — leetcode 4. Median of Two Sorted Arrays

    一道非常经典的题目,Median of Two Sorted Arrays.(PS:leetcode 我已经做了 190 道,欢迎围观全部题解 https://github.com/hanzichi/ ...

  2. LeetCode(3) || Median of Two Sorted Arrays

    LeetCode(3) || Median of Two Sorted Arrays 题记 之前做了3题,感觉难度一般,没想到突然来了这道比较难的,星期六花了一天的时间才做完,可见以前基础太差了. 题 ...

  3. Leetcode 4. Median of Two Sorted Arrays(二分)

    4. Median of Two Sorted Arrays 题目链接:https://leetcode.com/problems/median-of-two-sorted-arrays/ Descr ...

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

  5. LeetCode 4 Median of Two Sorted Arrays (两个数组的mid值)

    题目来源:https://leetcode.com/problems/median-of-two-sorted-arrays/ There are two sorted arrays nums1 an ...

  6. 第三周 Leetcode 4. Median of Two Sorted Arrays (HARD)

    4. Median of Two Sorted Arrays 给定两个有序的整数序列.求中位数,要求复杂度为对数级别. 通常的思路,我们二分搜索中位数,对某个序列里的某个数 我们可以在对数时间内通过二 ...

  7. LeetCode 4. Median of Two Sorted Arrays & 归并排序

    Median of Two Sorted Arrays 搜索时间复杂度的时候,看到归并排序比较适合这个题目.中位数直接取即可,所以重点是排序. 再来看看治阶段,我们需要将两个已经有序的子序列合并成一个 ...

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

  9. leetcode 4. Median of Two Sorted Arrays

    https://leetcode.com/problems/median-of-two-sorted-arrays/ There are two sorted arrays nums1 and num ...

  10. [LeetCode] 4. 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 ...

随机推荐

  1. SIGGRAPH2016【转】

    本文摘自:http://blog.selfshadow.com/ Open Access SIGGRAPH 2016 Conference Content (for a limited time) R ...

  2. Angular JS将数据显示为两列(html)

    (数据为Array数组)使用AngularJS中ng-show="{{}}",其将数据按行分为奇数行和偶数行,$even是判断是否为奇数行[如果是则为true,不是则为false] ...

  3. .NET 4.5+项目迁移.NET Core的问题记录

    .NET 4.5+项目迁移.NET Core的问题记录 这几天试着把目前的开发框架迁移到新的.net core平台,中间遇到的问题在这里简单记录一下. 迁移过程遇到的最大的问题IOC容器.我目前使用的 ...

  4. 「2014-2-8」Reading a blog on the pain points of Global Variables of C language

    晚上读到一篇<C 语言全局变量那些事儿>.我先前对链接的理解不深,算是涨了一番姿势.此文吐槽的重点,是「非 static 限定的全局变量」带来的看似出人意料(实则可以被合理解释)的行为.虽 ...

  5. [UCSD白板题] Pairwise Distinct Summands

    Problem Introduction This is an example of a problem where a subproblem of the corresponding greedy ...

  6. MOTION-MATCHING IN UBISOFT’S FOR HONOR翻译

    http://www.gameanim.com/2016/05/03/motion-matching-ubisofts-honor/ Introducing For Honor with a vide ...

  7. BZOJ2888 资源运输(LCT启发式合并)

    这道题目太神啦! 我们考虑他的每一次合并操作,为了维护两棵树合并后树的重心,我们只好一个一个的把节点加进去.那么这样一来看上去似乎就是一次操作O(nlogn),但是我们拥有数据结构的合并利器--启发式 ...

  8. Webalizer中文安装解析IP配置

    之前安装的都是英文的,现在替换成中文的,并且新增ip位置解析  参考地址 : http://haolulu.blog.51cto.com/3164472/630894 1.安装webalizer所需的 ...

  9. JAVA 正则表达式4种常用的功能

    下面简单的说下它的4种常用功能:   查询:   以下是代码片段: String str="abc efg ABC";    String regEx="a|f" ...

  10. Ubuntu之root权限的获取

    方案一: Ubuntu的root密码在没有设置之前是随机的,即在每一次开机的时候他的密码都不同,但是由于在安装Ubuntu的时候需要建立一个账户,而这个招呼又属于admin组,因此它可以对root进行 ...