貌似是去年阿里巴巴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. setenv.bat

    @echo off rem Licensed to the Apache Software Foundation (ASF) under one or more rem contributor lic ...

  2. dataGrid转换dataTable

    #region dataGrid转换dataTable   /// <summary>   /// dataGrid转换dataTable   /// </summary>   ...

  3. 嵌入式linux开发环境构建

    2.1硬件环境构建 2.1.1主机与目标板结合的交叉开发模式 在主机上编辑.编译软件,然后再目标办上运行.验证程序. 对于S3C2440.S3C2410开发板,进行嵌入式Linux开发时一般可以分为以 ...

  4. [XAF] Keep the DetailView open in a popup window

    public class ViewController1 : ViewController { ListViewProcessCurrentObjectController controller; p ...

  5. hdu 5719(Arrange)(冷静分析)

    A数组显示从0到i的最小值B数组显示从0到i的最大值由此可得:A数组是单调不增的(怎么也会不使得最小值变大)B数组是单调不减的.设premin和premax为i位以前的最小值和最大值.可以得出以下几点 ...

  6. LINUX下常用SHELL指令

    Linux Shell常用shell命令 一.文件.目录操作命令 1.ls命令 功能:显示文件和目录的信息 ls 以默认方式显示当前目录文件列表 ls -a 显示所有文件包括隐藏文件 ls -l 显示 ...

  7. OSD磁盘日常监控

    摘要:对ceph OSD磁盘,做好定期的性能数据采集和通电时长管理,长期的数据积累对磁盘的性能与生命周期管理会有一定帮助,同时也能确保整个集群性能的稳定. 磁盘碎片管理 查看磁盘碎片 # xfs_db ...

  8. lxc on centos

    终于把lxc的网络配通了,也不知道对不对,记一下 一开始都是雷同的地方 yum install libcgroup lxc lxc-templates 安装lxc cgroup 然后记得 chkcon ...

  9. 【转】Eclipse打JAR包,插件FatJar安装与使用

    原文地址:http://blog.csdn.net/jikeyzhang/article/details/4731968 下载RUL: 下载fatJar插件,解压缩后是一个.../plugins/(n ...

  10. 国庆送干货——前端建站实用UI工具vajoyJS

    差不多是从七月开始有空就写一写,写到现在也算是可以拿出来展示了,vajoyJS是一款可以提供多项建站常用UI功能的插件库,让你轻松创建简易幻灯片.模态窗口和单屏滚页等效果.vajoyJS依赖于 jQu ...