貌似是去年阿里巴巴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. GCD in Swfit 3.0

    这里包括了Queue, Group, Barrier, Semaphore等内容.基本上常用的GCD对象和方法在Swift3.0的改变都囊括其中. 代码在这里:https://github.com/f ...

  2. maven 搜索不到想从本地仓库依赖的jar包--重建本地maven仓库索引

    问题:有时候本地仓库有相关的jar包,但是在pom.xml中利用工具搜索不到 菜单栏设置显示Maven Respositorise视图:Windows--Show View--Other..   (对 ...

  3. C代码工具--自动生成enum值和名字映射代码

    这年头好像继续做C语言的人不多了,年轻人大多去互联网和移动应用.确实,那两个领域现在来钱快,且总是供不应求.就说刚刚在一个旧同事的微信群里,有人刚放出自己有团队可以做App几分钟,哇塞,好几个人说有项 ...

  4. [Leetcode][JAVA] Binary Tree Level Order Traversal

    Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...

  5. CodeSoft随笔 批量连续打印,变量打印,codesoft条码

    调用codeSoft的模板,实现批量连续打印. Code: 制作标签1.lab. 添加两个变量var0,var1. using LabelManager2; string strFile = Syst ...

  6. Await, and UI, and deadlocks! Oh my!

    It’s been awesome seeing the level of interest developers have had for the Async CTP and how much us ...

  7. bower 问题

    没法写成bower install jquery bootstrap:只能是bower install jquery; bower install bootstrap

  8. MYSQL删除表的记录后如何使ID从1开始

    MYSQL删除表的记录后如何使ID从1开始 MYSQL删除表的记录后如何使ID从1开始 http://hi.baidu.com/289766516/blog/item/a3f85500556e2c09 ...

  9. Git撤销提交

    本文链接:http://volnet.github.io/#!docs/git/reset-to-old-version.md 在使用Git进行版本管理的时候,经常会遇到一些错误的提交. 在开始演示之 ...

  10. Ubuntu Tftpd服务配置

    ---恢复内容开始--- 服务器端(ip:192.168.1.100) #安装tftpd-hpa sudo apt-get install tftpd-hpa 修改配置文件 sudo vim /etc ...