貌似是去年阿里巴巴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. 通过其他页面跳转到tableBar指示的界面

    通过代理实现 让tableBar遵从代理,让代理的实现方法设置 [self setSelectedIndex:2]; setSelectedIndex:2 代表tableBar的item第3个处于选中 ...

  2. Windows Phone 8.1 新特性 - 控件之列表选择控件

    本篇我们来介绍Windows Phone 8.1 新特性中的列表选择控件. 在Windows Phone 8 时代,大家都会使用 LongListSelector 来实现列表选择控件,对数据进行分组显 ...

  3. Windows 安装 openssl

    http://slproweb.com/products/Win32OpenSSL.html File Type Description Win32 OpenSSL v1.1.0b Light 3MB ...

  4. 精益VS六西格玛

    名称 精益方法 Six Sigma管理 假定 1)消除浪费可以改善绩效 2)大量的小改进更有利于组织成长 1)问题总是存在的: 2)测量是重要的: 3)随着变异减少,系统产出得到改进 文化基础 东方以 ...

  5. angularjs ocLazyLoad分步加载js文件,angularjs ocLazyLoad按需加载js

    用angular有一段时间了,平日里只顾着写代码,没有注意到性能优化的问题,而今有时间,于是捋了捋,讲学习过程记录于此: 问题描述:由于采用angular做了网页的单页面应用,需要一次性在主布局中将所 ...

  6. NSString格式校验

    在项目开发过程中,NSString类型的变量是经常用到的,而且我们常常会对其格式进行对应的各种校验,你比如,在登录注册的时候,需要验证用户名的长度,用户名的字符组成等等,其实现在也有很多第三方提供的N ...

  7. FLEXNET License管理

    之前的程序License管理是我自己手撸的一个非常简单的东东,根据用户机器的MAC地址生成一个字串,程序执行的时候去比较这个字串.当时只是追求一个最简单的实现,像证书过期.功能点证书自然没有.这次新版 ...

  8. Manifesto of the Communist Party

    A spectre is haunting Europe – the spectre of communism. All the powers of old Europe have entered i ...

  9. C# 调用restful服务开源库

    .NET环境下我们想调用其它开放平台的服务接口,不需要自己去实现底层,开源的库用起来会很方便 hammock http://www.cnblogs.com/shanyou/archive/2012/0 ...

  10. OAuth 2.0 授权原理

    出处:http://www.cnblogs.com/neutra/archive/2012/07/26/2609300.html 最近在做第三方接入的,初步定下使用OAuth2协议,花了些时间对OAu ...