LeetCode | Median of Two Sorted Arrays
来源:互联网 发布:淘宝哪家洗照片质量好 编辑:程序博客网 时间:2024/04/28 02:33
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)).
一开始没看清题目,以为O(m+n)的时间复杂度。。。,然后就直接两个指针从数组头开始指向了,结果正常地超时了。
明显是用二分法。
这题其实还是求第K个数,假设两个数组A,B.长度大于k,比较A[k/2 - 1]和B[k/2 - 1]的大小。
三种情况:如果相等,最好了,找到了所求;
A[k/2 - 1]<B[k/2 - 1],那么A[k/2 -1]和它之前的元素可以不用找了,不会在那里的,排除他们。这时候k = k - (k/2 -1)即把不可能的数排除后,k的值也在变,这样才可以递归嘛。
A[k/2 - 1]>B[k/2 - 1] 同理。。。
做算法题,还是要注意边界值!!!思考的时候也最好举例子在草稿一边演算,这样思路明确,更加清晰。
网上有段代码写的优美,条理清晰,效率恐怖:
double findKth(int a[], int m, int b[], int n, int k)
{
//always assume that m is equal or smaller than n
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]);
//divide k into two parts
int pa = min(k / 2, m), pb = k - pa;
if (a[pa - 1] < b[pb - 1])
return findKth(a + pa, m - pa, b, n, k - pa);
else if (a[pa - 1] > b[pb - 1])
return findKth(a, m, b + pb, n - pb, k - pb);
else
return a[pa - 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;
}
};
{
//always assume that m is equal or smaller than n
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]);
//divide k into two parts
int pa = min(k / 2, m), pb = k - pa;
if (a[pa - 1] < b[pb - 1])
return findKth(a + pa, m - pa, b, n, k - pa);
else if (a[pa - 1] > b[pb - 1])
return findKth(a, m, b + pb, n - pb, k - pb);
else
return a[pa - 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;
}
};
0 0
- LeetCode: Median of Two Sorted Arrays
- LeetCode Median of Two Sorted Arrays
- leetcode 26: Median of Two Sorted Arrays
- Leetcode 4 Median of Two Sorted Arrays
- [Leetcode] Median of Two Sorted Arrays
- LeetCode 4 - Median of Two Sorted Arrays
- [LeetCode]Median of Two Sorted Arrays
- leetcode Median of Two Sorted Arrays
- LeetCode-Median of Two Sorted Arrays
- [LeetCode] Median of Two Sorted Arrays
- [LeetCode] Median of Two Sorted Arrays
- [leetcode] Median of Two Sorted Arrays
- leetcode-004:Median of Two Sorted Arrays
- Leetcode 4 Median of Two Sorted Arrays
- leetcode之 median of two sorted arrays
- 【转载】【leetcode】Median of Two Sorted Arrays
- LeetCode - Median of Two Sorted Arrays
- [LeetCode]Median of Two Sorted Arrays
- 又一个程序猿的奋斗史——第六章最后的两个星期?
- ubuntu mentohust链接校园网
- SimpleDateFormat使用
- ffmpeg中的sws_scale算法性能测试
- 又一个程序猿的奋斗史——第七章一些小插曲
- LeetCode | Median of Two Sorted Arrays
- mysql 登录问题的解决办法和一些命令
- MySQL企业级高可用性
- Matlab与Java混合编程
- 属性修饰
- Storyboard的使用
- 基于ONOS的SDN-IP架构概述篇
- Android中getLocationOnScreen和getLocationInWindow
- pmwiki 表格