Leetcode 4 Median of Two Sorted Arrays 二分查找(二分答案+二分下标)
貌似是去年阿里巴巴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 二分查找(二分答案+二分下标)的更多相关文章
- 【算法之美】求解两个有序数组的中位数 — leetcode 4. Median of Two Sorted Arrays
一道非常经典的题目,Median of Two Sorted Arrays.(PS:leetcode 我已经做了 190 道,欢迎围观全部题解 https://github.com/hanzichi/ ...
- LeetCode(3) || Median of Two Sorted Arrays
LeetCode(3) || Median of Two Sorted Arrays 题记 之前做了3题,感觉难度一般,没想到突然来了这道比较难的,星期六花了一天的时间才做完,可见以前基础太差了. 题 ...
- Leetcode 4. Median of Two Sorted Arrays(二分)
4. Median of Two Sorted Arrays 题目链接:https://leetcode.com/problems/median-of-two-sorted-arrays/ Descr ...
- 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 ...
- LeetCode 4 Median of Two Sorted Arrays (两个数组的mid值)
题目来源:https://leetcode.com/problems/median-of-two-sorted-arrays/ There are two sorted arrays nums1 an ...
- 第三周 Leetcode 4. Median of Two Sorted Arrays (HARD)
4. Median of Two Sorted Arrays 给定两个有序的整数序列.求中位数,要求复杂度为对数级别. 通常的思路,我们二分搜索中位数,对某个序列里的某个数 我们可以在对数时间内通过二 ...
- LeetCode 4. Median of Two Sorted Arrays & 归并排序
Median of Two Sorted Arrays 搜索时间复杂度的时候,看到归并排序比较适合这个题目.中位数直接取即可,所以重点是排序. 再来看看治阶段,我们需要将两个已经有序的子序列合并成一个 ...
- 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 ...
- leetcode 4. Median of Two Sorted Arrays
https://leetcode.com/problems/median-of-two-sorted-arrays/ There are two sorted arrays nums1 and num ...
- [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 ...
随机推荐
- Python生成器的经典程序
import random def get_data(): """返回0到9之间的3个随机数""" return random.sample ...
- WPF Touch操作滚动条,Window弹跳
WPF,用ScrollViewer控件,触屏开发,当滑动到最后时会使整个窗体弹跳一下 原因是因为ScrollViewer触屏操作原生支持惯性,ScrollViewer中的内容滚动到边界是会自动触发Wi ...
- cocos2d 3.X Shader 变暗和变灰
转自http://www.waitingfy.com/archives/1741 1.为了节约一张图引发的Shader使用 我们注意到这个游戏当中经常使用一些按钮,美术会给两张图,一张稍微暗点,表示 ...
- Postman-进阶
Postman-简单使用 Postman-进阶使用 Postman-CI集成Jenkins 管理请求 保存请求-添加“打开百度首页请求” 设置请求方式为Get,地址为www.baidu.com.点击右 ...
- JAVA/Android Map与String的转换方法
在Android开发中 Map与String的转换在,在一些需求中经常用到,使用net.sf.json.JSONObject.fromObject可以方便的将string转为Map.但需要导入jar包 ...
- 杨光福IT讲师微博
杨光福IT讲师微博: http://weibo.com/321chinavideo 微博现在里面有很多干货,以后会越来越多,主要用于分享和交流技术.关注一下对你有帮助.
- 【转】Backbone使用总结
转自 http://www.pchou.info/javascript/2014/06/26/backbone-summary-01.html 开始在项目中大规模使用backbone,一路磕磕碰碰, ...
- 最快让你上手ReactiveCocoa之进阶篇
前言 由于时间的问题,暂且只更新这么多了,后续还会持续更新本文<最快让你上手ReactiveCocoa之进阶篇>,目前只是简短的介绍了些RAC核心的一些方法,后续还需要加上MVVM+Rea ...
- Razor 中的@helper 与 @function 用法
@helper : 可以有返回值,也可以没有返回值 @function :需要有返回值 可以将View中公共部分的代码抽取出来,变成一个独立的方法 公共部分 view 抽出的公共部分 ...
- windows系统下的第一个console程序
窗口+r 键,输入cmd,打开一个命令行窗口 切换到你的目标目录 输入 dotnet new dotnet会自动帮你创建3个文件. NuGet.Config文件主要定义了NuGet获取nupkg包时的 ...