[itint5]两有序数组的中位数
这个题和leetcode的基本一样。用了更好点的思路。在A中折半猜是不是中位数,A中没有后在B中猜。最后猜到B[j]<=A[i]<=B[j+1],此时,无论奇偶(2k+1或者2k个),A[i]都是第k+1那个。那么奇数时,A[i]是正中的那个;偶数时,A[i]是中位数两个里大的那个,小的那个要从B[j]和A[i-1]里选一个。
要注意的是A和B可能为空。而且要注意偶数的情况是,最后算出来的两个,要先判断位置j和i-1是否存在。
#include <climits>
int median(vector<int> &A) {
int n = A.size();
if (n % 2 == 1) {
return A[n/2];
} else {
return (A[n/2-1] + A[n/2]) / 2;
}
}
int median(vector<int> &A, vector<int> &B, int l, int r) {
if (l > r) return median(B, A, 0, B.size()-1);
int an = A.size();
int bn = B.size();
int i = (l + r) / 2;
int j = (an + bn) / 2 - i - 1;
if (j >= 0 && A[i] < B[j]) {
return median(A, B, i+1, r);
} else if (j < bn - 1 && A[i] > B[j+1]) {
return median(A, B, l, i-1);
} else {
if ((an + bn) % 2 == 1) {
return A[i];
} else {
int another = INT_MIN;
if (j >= 0 && j < B.size()) {
another = max(another, B[j]);
}
if (i-1 > 0 && i-1 < A.size()) {
another = max(A[i-1], another);
}
return (A[i] + another) / 2;
}
}
}
int median(vector<int> &arr1, vector<int> &arr2) {
if (arr1.size() == 0) return median(arr2);
if (arr2.size() == 0) return median(arr1);
return median(arr1, arr2, 0, arr1.size()-1);
}
[itint5]两有序数组的中位数的更多相关文章
- [itint5]两有序数组的交和并
这一题也简单,唯一有意思的地方是提炼了一个函数用来做数组索引去重前进. int forward(vector<int> &arr, int i) { while (i+1 < ...
- [LeetCode] 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 ...
- leetcode刷题四<寻找两个有序数组的中位数>
给定两个大小为 m 和 n 的有序数组 nums1 和 nums2. 请你找出这两个有序数组的中位数,并且要求算法的时间复杂度为 O(log(m + n)). 你可以假设 nums1 和 nums2 ...
- LeetCode练习4 找出这两个有序数组的中位数
给定两个大小为 m 和 n 的有序数组 nums1 和 nums2. 请你找出这两个有序数组的中位数,并且要求算法的时间复杂度为 O(log(m + n)). 你可以假设 nums1 和 nums2 ...
- [LeetCode] 4. 寻找两个有序数组的中位数
题目链接:https://leetcode-cn.com/problems/median-of-two-sorted-arrays/ 题目描述: 给定两个大小为 m 和 n 的有序数组 nums1 和 ...
- 两个有序数组的中位数(第k大的数)
问题:两个已经排好序的数组,找出两个数组合并后的中位数(如果两个数组的元素数目是偶数,返回上中位数). 感觉这种题目挺难的,尤其是将算法完全写对.因为当初自己微软面试的时候遇到了,但是没有想出来思路. ...
- leetcode第四题:两个有序数组的中位数
给定两个大小为 m 和 n 的有序数组 nums1 和 nums2. 请你找出这两个有序数组的中位数,并且要求算法的时间复杂度为 O(log(m + n)). 你可以假设 nums1 和 nums2 ...
- 【LeetCode】4. 寻找两个有序数组的中位数
给定两个大小为 m 和 n 的有序数组 nums1 和 nums2. 请你找出这两个有序数组的中位数,并且要求算法的时间复杂度为 O(log(m + n)). 你可以假设 nums1 和 nums2 ...
- 求两个有序数组的中位数或者第k小元素
问题:两个已经排好序的数组,找出两个数组合并后的中位数(如果两个数组的元素数目是偶数,返回上中位数). 设两个数组分别是vec1和vec2,元素数目分别是n1.n2. 算法1:最简单的办法就是把两个数 ...
随机推荐
- Libcurl笔记二
一: multi与easy接口的不同处The multi interface offers several abilities that the easy interface doesn't. The ...
- C++ sizeof操作符的用法和strlen函数的区别
摘要:本人首先介绍了C++中sizeof操作符的用法和注意事项,其次对比了和strlen的区别和使用,方便大家在写代码的时候查阅,和面试.笔试的时候复习. 目录: sizeof的用法: sizeof和 ...
- centos7搭建NIS与NFS综合应用
实验环境: centos7(服务端) redhat enterprise linux 7.2(客户端) 实验目的:用centos7的账号,能在redhat enterprise linu ...
- 隐藏win7盘符
1.隐藏盘符: //新建注册表,隐藏X盘符 int regeditme() { HKEY hkey; DWORD dwLastError= ;//隐藏X盘2^25 J:2^9=512 X:盘符与挂载的 ...
- 使用c#检测文件正在被那个进程占用
要检测文件被那个进程占用,需要使用微软提供的工具Handle.exe,这里有微软提供的下载 我们可以在c#中调用Handle.exe 来检测到底哪个进程占用了文件 string fileName = ...
- [OC] UITabBarController
1. New CocoaTouch class -> Select Objective C -> named RootViewController 2. Disable APC error ...
- Objective-C中class、Category、Block的介绍
@class 当定义一个类,必须为编译器提供两组消息,第一组(接口部分.h):构建类的实例的一个基本蓝图.必须指定类名,类的超类,类的实例变量和类型的列表,最后是类的方法的声明.第二组(实现部分.m) ...
- Nagios脚本编写事例
目标:编写一个简单的nagios脚本,实现监控client上的nginx进程是否启动,假如没启动的话发出报警. 首先在master上对nagios的配置文件进行设置,修改services.cfg文件, ...
- 使用OpenXml把Excel中的数据导出到DataSet中
public class OpenXmlHelper { /// <summary> /// 读取Excel数据到DataSet中,默认读取所有Sheet中的数据 /// </sum ...
- UIImagePickerController拍照与摄像(转)
转载自:http://blog.sina.com.cn/s/blog_68edaff101019ppe.html (2012-11-23 14:38:40) 标签: ios iphone 拍照 摄像 ...