【Median of Two Sorted Arrays】cpp
题目:
There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).
代码:
class Solution {
public:
double findMedianSortedArrays(int A[], int m, int B[], int n)
{
int total = m+n;
if(total & 0x1)
{
return Solution::find_kth(A,m,B,n,(m+n)/+);
}
else
{
return ( Solution::find_kth(A,m,B,n,(m+n)/) + Solution::find_kth(A,m,B,n,(m+n)/+) ) / 2.0;
}
}
static int find_kth(int A[], int m, int B[], int n, int k)
{
// make sure m is less or equal than n
if(m>n) return find_kth(B,n,A,m,k);
// finish conditions
if (m==) return B[k-];
if (k==) return std::min(A[],B[]);
// binary search
int pos_A = std::min(k/,m);
int pos_B = k - pos_A;
if (A[pos_A-] < B[pos_B-])
{
return Solution::find_kth(A+pos_A, m-pos_A, B, n, k-pos_A);
}
else if (A[pos_A-] > B[pos_B-])
{
return Solution::find_kth(A, m, B+pos_B, n-pos_B, k-pos_B);
}
else
{
return A[pos_A-];
}
}
};
Tips:
1. 采用二分查找,需要判断一下边界条件。
2. 这里用到一个技巧,始终保证m小于n,如果不满足就做一次转换
========================================
第二次过这道题,OJ的接口已经改成vector的了。
第一遍能记得大概的思路,但是涉及到边界细节,还是有些无力。
class Solution {
public:
double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
const int m = nums1.size();
const int n = nums2.size();
int k = m+n;
if ( k & ) // odd
{
return Solution::findMedian(nums1, , m-, nums2, , n-, (m+n)/+);
}
else // even
{
return (
Solution::findMedian(nums1,, m-, nums2, , n-, (m+n)/) +
Solution::findMedian(nums1, , m-, nums2, , n-, (m+n)/+) ) /2.0;
}
}
static double findMedian(
vector<int>& nums1, int begin1, int end1,
vector<int>& nums2, int begin2, int end2,
int k
)
{
// make sure "end1-begin1" <= "end2-begin2"
if ( end1-begin1 > end2-begin2 )
{
return Solution::findMedian(nums2, begin2, end2, nums1, begin1, end1, k);
}
// finish conditions
if ( begin1>end1 ) return nums2[begin2+k-];
if ( k== ) return std::min(nums1[begin1], nums2[begin2]);
// recursive branchs
int local1 = std::min(k/, end1-begin1+);
int local2 = k-local1;
if ( nums1[begin1+local1-]<nums2[begin2+local2-] )
{
return Solution::findMedian(nums1, begin1+local1, end1, nums2, begin2, end2, local2);
}
else if ( nums1[begin1+local1-]>nums2[begin2+local2-] )
{
return Solution::findMedian(nums1, begin1, end1, nums2, begin2+local2, end2, local1);
}
else
{
return nums1[begin1+local1-];
}
}
};
tips:
主要是几个细节
1. 长度是奇数和偶数要分别处理:对于奇数长度的median就是中间的那个元素;对于偶数长度的median就是中间那两个位置元素的均值
2. 既然是递归就一定要有终止条件,这里的终止条件有两个:
a) 有一个数组走到头了
b) 还需要挑出来的元素个数为1(即k==1)
遇上上述两个条件,递归可以不用往下进行了。
3. 还是强调一个技巧,需要判断两个数组长短的时候,不如再一开始就限定好传进来的哪个数组长,哪个数组短
即,
// make sure "end1-begin1" <= "end2-begin2"
if ( end1-begin1 > end2-begin2 )
{
return Solution::findMedian(nums2, begin2, end2, nums1, begin1, end1, k);
}
这样一来就省去了很多的代码(比如已经知道了nums1的长度一定小于nums2的长度,就可以得到local1了)
4. 以前有个思维限制,既然binary search就一定要每次排除k个,那么nums1要排除k/2个,nums2要排除k/2个喽。。。这是个比较大的思维误区,由于是两个数组,每个多长都不一定,不一定两个数组都干掉相同长度;况且,k可能是奇数或者偶数,讨论起来特别麻烦。
因此,干脆确定短的一个数组往后推的长度,再用总共需要挑出来的数字减去数组一用过的长度,剩下的就是第二个数组可以往后推的长度了。
这道题非常好 思路+细节都有值得学习的地方,虽然是第二次AC了,但还是觉得受益。
【Median of Two Sorted Arrays】cpp的更多相关文章
- 【Remove Duplicates from Sorted List 】cpp
题目: 第一次刷的时候漏掉了这道题. Given a sorted linked list, delete all duplicates such that each element appear o ...
- 【Search In Rotated Sorted Array】cpp
题目: Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 ...
- 【Remove Duplicates from Sorted Array】cpp
题目: https://leetcode.com/problems/remove-duplicates-from-sorted-array/ Given a sorted array, remove ...
- 【算法之美】求解两个有序数组的中位数 — leetcode 4. Median of Two Sorted Arrays
一道非常经典的题目,Median of Two Sorted Arrays.(PS:leetcode 我已经做了 190 道,欢迎围观全部题解 https://github.com/hanzichi/ ...
- 【转载】两个排序数组的中位数 / 第K大元素(Median of Two Sorted Arrays)
转自 http://blog.csdn.net/zxzxy1988/article/details/8587244 给定两个已经排序好的数组(可能为空),找到两者所有元素中第k大的元素.另外一种更加具 ...
- 【LeetCode】4. Median of Two Sorted Arrays (2 solutions)
Median of Two Sorted Arrays There are two sorted arrays A and B of size m and n respectively. Find t ...
- LeetCode 4. Median of Two Sorted Arrays & 归并排序
Median of Two Sorted Arrays 搜索时间复杂度的时候,看到归并排序比较适合这个题目.中位数直接取即可,所以重点是排序. 再来看看治阶段,我们需要将两个已经有序的子序列合并成一个 ...
- 《LeetBook》leetcode题解(4): Median of Two Sorted Arrays[H]——两个有序数组中值问题
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- 4. Median of Two Sorted Arrays(topK-logk)
4. Median of Two Sorted Arrays 题目 There are two sorted arrays nums1 and nums2 of size m and n respec ...
随机推荐
- 提升Web性能的8个技巧总结
提升Web性能的8个技巧总结 在互联网盛行的今天,越来越多的在线用户希望得到安全可靠并且快速的访问体验.针对Web网页过于膨胀以及第三脚本蚕食流量等问题,Radware向网站运营人员提出以下改进建议, ...
- bzoj3242 [Noi2013]快餐店
Description 小T打算在城市C开设一家外送快餐店.送餐到某一个地点的时间与外卖店到该地点之间最短路径长度是成正比的,小T希望快餐店的地址选在离最远的顾客距离最近的地方. 快餐店的顾客分布在城 ...
- iterable -------JavaScript
本文摘要:http://www.liaoxuefeng.com/ 遍历Array可以采用下标循环,遍历Map和Set就无法使用下标.为了统一集合类型,ES6标准引入了新的iterable类型,Arra ...
- Ubuntu 14.04 LTS 触摸板无法使用
c16b上,触摸板不能使用,查找后发现,需要在加载驱动时增加参数. 如下所说: 1.使用以下命令后,触摸板可以使用 sudo modprobe -r psmouse sudo modprobe psm ...
- C盘扩展卷是灰色的扩容方法
当想要扩容C盘的时候可能会发现C盘的扩展卷竟然是灰色的.原因是C盘旁边没有紧挨着的“”未分配空间“”, 只要将D盘的空间分出一些来就可以了. !!!磁盘的分区合并有风险,重要文件等记得先备份 !!! ...
- Zabbix监控告警Lack of free swap space on Zabbix server解决办法
报错详情如下: 是因为Zabbix监控没有考虑虚拟主机的交换空间情况 解决办法修改配置 修改表达式内容:{Template OS Linux:system.swap.size[,pfree].last ...
- django 图片上传与显示
由于图片上传的需要,学习了一波上传 1. 上传 前端代码 <form action="写上相应的定向位置" method="post" enctype=& ...
- http 高级配置 虚拟主机,https 编译安装
目录 http 高级配置 虚拟主机,https 编译安装 http 重定向 https HSTS HSTS preload list http 自带的工具程序 httpd的压力测试工具 实现状态页 反 ...
- Openstack搭建(流水账)
Openstack管理三大资源:1.网络资源2.计算资源3.存储资源 Keystone 做服务注册 Glance 提供镜像服务 Nova 提供计算服务 Nova scheduler决策虚拟主机创建在哪 ...
- Vue 使用History记录上一页面的数据
UI Mvvm 前端数据流框架精讲 Vue数据双向绑定探究 面试问题:Vuejs如何实现双向绑定 数据双向绑定的探究和实现 需求 从列表页的第二页进入详情页,返回时列表页仍然显示在第二页: 从列表页的 ...