LeetCode 4 Median of Two Sorted Array
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)).
这个题是求两个已排数组的中位数,更一般的就是求两个已排数组的第k大的数。
自己想到的解决方法有一个先用合并排序将两个数组进行排序得到一个排列后的数组,在遍历找到第k到的数,但是这样算法的复杂度是o(nlogn),显然不符合题目的要求。
参考http://blog.csdn.net/zxzxy1988/article/details/8587244中的解法:
假设A,B两个数组元素都大于k/2。可以比较A[k/2]和B[k/2]
(1)A[k/2-1] < B[k/2-1] 此时可以判断A[0]~A[k/2]均比最终要找的median要小,可以去掉这部分元素。
(2)A[k/2-1] > B[k/2-1]此时可以判断B[0]~B[k/2]均比最终要找的median要小,可以去掉这部分元素。
(3)A[k/2-1] = B[k/2-1]此时判断要找的median就是A[k/2-1]orB[k/2-1].
使用上面的结论,还要考虑边界情况。
实现代码如下
class Solution {
public:
double findMedianSortedArrays(int a[], int m, int b[], int n)
{
int total = m + n;
if(total & 0x1)
{
return findkth(a, m, b, n, total / 2 + 1);
}
else
{
return (findkth(a, m, b, n, total / 2) +findkth(a, m, b,n, total /2 + 1)) / 2;
}
}
double findkth(int a[], int m, int b[], int n, int k)
{
if(m > n)
return findkth(b, n, a, m, k);
if(m == 0)
return b[k-1];
if(k <= 1)
return min(a[0], b[0]);
int idxA = min(k/2, m);
int idxB = k - idxA;
if(a[idxA - 1] < b[idxB-1])
{
return findkth(a+idxA, m-idxA, b, n, k- idxA);
}
else if(b[idxB-1] < a[idxA-1])
{
return findkth(a, m, b+ idxB, n - idxB, k - idxB );
}
else
return a[idxA - 1];
}
};
使用的迭代,考虑到求解的方便使用:
if(m > n)
return findkth(b, n, a, m, k);//这句使得后面具体结果讨论省了一半,32个赞
if(m == 0)
return b[k-1];
if(k <= 1)
return min(a[0], b[0]);
对边界情况进行讨论。
多看多学习,不管怎样,这是个好时代,加油。
LeetCode 4 Median of Two Sorted Array的更多相关文章
- 【算法之美】求解两个有序数组的中位数 — leetcode 4. Median of Two Sorted Arrays
一道非常经典的题目,Median of Two Sorted Arrays.(PS:leetcode 我已经做了 190 道,欢迎围观全部题解 https://github.com/hanzichi/ ...
- LeetCode(3) || Median of Two Sorted Arrays
LeetCode(3) || Median of Two Sorted Arrays 题记 之前做了3题,感觉难度一般,没想到突然来了这道比较难的,星期六花了一天的时间才做完,可见以前基础太差了. 题 ...
- [LeetCode] 033. Search in Rotated Sorted Array (Hard) (C++)
指数:[LeetCode] Leetcode 解决问题的指数 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 033. ...
- [array] leetcode - 33. Search in Rotated Sorted Array - Medium
leetcode - 33. Search in Rotated Sorted Array - Medium descrition Suppose an array sorted in ascendi ...
- LeetCode 81 Search in Rotated Sorted Array II [binary search] <c++>
LeetCode 81 Search in Rotated Sorted Array II [binary search] <c++> 给出排序好的一维有重复元素的数组,随机取一个位置断开 ...
- LeetCode 33 Search in Rotated Sorted Array [binary search] <c++>
LeetCode 33 Search in Rotated Sorted Array [binary search] <c++> 给出排序好的一维无重复元素的数组,随机取一个位置断开,把前 ...
- LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++>
LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++> 给出排序好的一维数组,如果一个元素重复出现的次数 ...
- LeetCode 26 Remove Duplicates from Sorted Array [Array/std::distance/std::unique] <c++>
LeetCode 26 Remove Duplicates from Sorted Array [Array/std::distance/std::unique] <c++> 给出排序好的 ...
- [LeetCode] 80. Remove Duplicates from Sorted Array II ☆☆☆(从有序数组中删除重复项之二)
https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/discuss/27976/3-6-easy-lines-C% ...
随机推荐
- 做小Leader的心得体会
只是自己的工作心得体会,代码属于也不够专业,大家不喜勿喷. 8月份来到这家新公司,没过一个月给派了个活:带着两个小弟给某银行开发一个小工具.功能很简单,就是用Java做一个windows上的C端工具, ...
- 解决灰色shader与mask冲突的方案
Shader "Custom/Opaque" { Properties { [PerRendererData] _MainTex ("Sprite Texture&quo ...
- vue路由文档笔记
引入router this.$router 和 router 使用起来完全一样.我们使用 this.$router 的原因是我们并不想在每个独立需要封装路由的组件中都导入路由 可以在任何组件内通过 t ...
- 【leetcode】19. 删除链表的倒数第N个节点
描述 给定一个链表,删除链表的倒数第 n 个节点,并且返回链表的头结点. 示例 给定一个链表: 1->2->3->4->5, 和 n = 2. 当删除了倒数第二个节点后,链表变 ...
- NLP系列-中文分词(基于统计)
上文已经介绍了基于词典的中文分词,现在让我们来看一下基于统计的中文分词. 统计分词: 统计分词的主要思想是把每个词看做是由字组成的,如果相连的字在不同文本中出现的次数越多,就证明这段相连的字很有可能就 ...
- 关于Scala文件操作中出现的问题
在各种项目中,我们常常需要用到文件操作,笔者在近期的项目中遇到了一个与文件操作相关的问题. 在代码实现的过程中,笔者首先定义了一个文件路径:def PATH = "/a/b/c.txt&qu ...
- DFS(2)——hdu1241Oil Deposits
一.题目回顾 题目链接:Oil Deposits 题意:给你一块网格,网格被分为面积相等的地块,这些地块中标记'@'的是油田,标记'*'的不是油田.已知一块油田与它上下左右以及对角线的油田同属一片油区 ...
- java面笔准备
这套面试题主要目的是帮助那些还没有java软件开发实际工作经验,而正在努力寻找java软件开发工作的朋友在笔试时更好地赢得笔试和面试.由于这套面试题涉及的范围很泛,很广,很杂,大家不可能一天两天就看完 ...
- 微信小程序—setTimeOut定时器的坑
原文地址: http://fanjiajia.cn/2018/06/27/%E5%BE%AE%E4%BF%A1%E5%B0%8F%E7%A8%8B%E5%BA%8F%E2%80%94setTimeOu ...
- fork开源代码后如何基于某个tag建立自己的branch
应用场景: 在github上fork一个自己想看的开源项目,想基于某个tag来写一些测试demo,然后可以做到版本控制. 方法: //克隆 git clone xxxxx.git //查看tag gi ...