[LintCode] 两个排序数组的中位数
class Solution {
public:
/**
* @param A: An integer array.
* @param B: An integer array.
* @return: a double whose format is *.5 or *.0
*/
double findMedianSortedArrays(vector<int> A, vector<int> B) {
// write your code here
int m = A.size(), n = B.size();
if (m > n) return findMedianSortedArrays(B, A);
int imin = , imax = m, half = (m + n + ) / , i, j, num1, num2;
while (imin <= imax) {
i = (imin + imax) / ;
j = half - i;
if (j > && i < m && B[j - ] > A[i])
imin = i + ;
else if (i > && j < n && A[i - ] > B[j])
imax = i - ;
else {
if (!i) num1 = B[j - ];
else if (!j) num1 = A[i - ];
else num1 = max(A[i - ], B[j - ]);
break;
}
}
if ((m + n) % ) return num1;
if (i == m) num2 = B[j];
else if (j == n) num2 = A[i];
else num2 = min(A[i], B[j]);
return (num1 + num2) / 2.0;
}
};
[LintCode] 两个排序数组的中位数的更多相关文章
- 2.Median of Two Sorted Arrays (两个排序数组的中位数)
要求:Median of Two Sorted Arrays (求两个排序数组的中位数) 分析:1. 两个数组含有的数字总数为偶数或奇数两种情况.2. 有数组可能为空. 解决方法: 1.排序法 时间复 ...
- LeetCode-4. 两个排序数组的中位数(详解)
链接:https://leetcode-cn.com/problems/median-of-two-sorted-arrays/description/ 有两个大小为 m 和 n 的排序数组 nums ...
- JavaScript实现获取两个排序数组的中位数算法示例
本文实例讲述了JavaScript排序代码实现获取两个排序数组的中位数算法.分享给大家供大家参考,具体如下: 题目 给定两个大小为 m 和 n 的有序数组 nums1 和 nums2 . 请找出这两个 ...
- LeetCode(4):两个排序数组的中位数
Hard! 题目描述: 有两个大小为 m 和 n 的排序数组 nums1 和 nums2 . 请找出两个排序数组的中位数并且总的运行时间复杂度为 O(log (m+n)) . 示例 1: nums1 ...
- LeetCode4. 两个排序数组的中位数
4. 两个排序数组的中位数 问题描述 There are two sorted arrays nums1 and nums2 of size m and n respectively.Find the ...
- Leetcode4--->求两个排序数组的中位数
题目:给定两个排序数组,求两个排序数组的中位数,要求时间复杂度为O(log(m+n)) 举例: Example 1: nums1 = [1, 3] nums2 = [2] The median is ...
- 从0打卡leetcode之day 5 ---两个排序数组的中位数
前言 我靠,才坚持了四天,就差点不想坚持了.不行啊,我得把leetcode上的题给刷完,不然怕是不好进入bat的大门. 题目描述 给定两个大小为 m 和 n 的有序数组 nums1 和 nums2 . ...
- leetcode 4.两个排序数组的中位数
题目: 给定两个大小为 m 和 n 的有序数组 nums1 和 nums2 . 请找出这两个有序数组的中位数.要求算法的时间复杂度为 O(log (m+n)) . 你可以假设 nums1 和 nums ...
- leetcode,两个排序数组的中位数
先上题目描述: 给定两个大小为 m 和 n 的有序数组 nums1 和 nums2 . 请找出这两个有序数组的中位数.要求算法的时间复杂度为 O(log (m+n)) . 你可以假设 nums1 和 ...
随机推荐
- css3 使用animation实现动画效果
.rotation { -webkit-animation-name: rotation; -webkit-animation-duration: 30s; -moz-animation-name: ...
- SQL中intersect、union、minus和except 运算符
1.intersect运算符intersect运算符通过只包括 TABLE1 和 TABLE2 中都有的行并消除所有重复行而派生出一个结果表.当 ALL 随 INTERSECT 一起使用时 (inte ...
- SSM整合开发流程
我的spring是3.2,mybatis是3.4 1 引入user libarary,我的jar文件如下 //spring mvc core springMVC\spring-web-.RELEASE ...
- C# DateTime 时间格式
//2008年4月24日 System.DateTime.Now.ToString("D"); //2008-4-24 System.DateTime.Now.ToString(& ...
- Xcode 警告信息处理:Format string is not a string literal (potentially insecure)
转自:http://www.oschina.net/question/54100_33881 NSObject *obj = @"A string or other object." ...
- CSS学习笔记(3)--表格边框
http://www.alixixi.com/web/a/2009082657736.shtml 对于很多初学HTML的人来说,表格<table>是最常用的标签了,但对于表格边框的控制,很 ...
- 使用心跳机制实现CS架构下多客户端的在线状态实时更新以及掉线自动重连
此文讲述的内容是一个实际项目开发中的一部分内容,笔者将亲身经历写成文章. [背景] 现需要实现这样的功能:有多个客户端连着同一个服务器.服务器和客户端之间需要“互相”知道彼此的连接状态.比如在某一时刻 ...
- vim中ctags应用
ctags(Generate tag files for source code)是vim下方便代码阅读的工具.尽管ctags也可以支持其它编辑器,但是它正式支持的只有VIM.并且VIM中已经默认安装 ...
- linux web.py spawn-fcgi web.py 配置
本来要用uwsgi,但是...介于以前说过...这台服务器略老...redhat 3的系统...确实很老,没法用yum,没法安装很多东西,打算自己编译uwsgi,但是编译各种错误...花了快一天,最后 ...
- Acme Corporation UVA - 11613 拆点法+最大费用最大流(费用取相反数)+费用有正负
/** 题目:Acme Corporation UVA - 11613 拆点法+最大费用最大流(费用取相反数)+费用有正负 链接:https://vjudge.net/problem/UVA-1161 ...