LeetCode 题解(66): Median of Two Sorted Arrays
来源:互联网 发布:豆丁免费下载软件 编辑:程序博客网 时间:2024/05/30 07:13
题目:
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)).
题解:
找 m + n 个数的中位数,若 m + n 为偶数,则相当于找第(m+n)/2大的数,与第(m+n) / 2 + 1大的数,然后相加除以2.
若 m + n 为奇数,则找中位数,相当于找其中第 (m + n)/ 2 + 1大的数。
问题转化为如何快速找到第 k 大的数。
解法为,比较 A 的 第 x 个数(A[x-1])和 B 的第 k - x 个数(B[k-x-1]), x + (k-x) = k,若A[x-1] < B[k-x-1],则A[x-1]必不可能为第k大的数,证明可见姚光超的专栏。同理,若A[x-1] > B[k-x-1],则B[k-x-1]必不可能为第k大的数,而当两数相等时,两数必同时为第k大的数。知道必不可能的目的是,可以直接将不可能的数排除在考虑范围之外,比如,A[x-1] < B[k-x-1],则直接将A中的前x个数剔除,同时将寻找第k大数,改为在剔除后的两个新数组中寻找第k - x 大的数。
还有几个特殊情况:
1. 为了保持代码简洁,将问题简化为A的大小永远小于B的大小,如A大,将AB交换后重新计算。
2. 在A为少数数组的情况下,若A为空,则第k大的数为B[k-1],直接返回B[k-1]。
3. 若k=1,则返回A[0]和B[0]中小的那个。
c++版:
class Solution {public: double findMedianSortedArrays(int A[], int m, int B[], int n) { if((m + n) % 2) { return findKth(A, m, B, n, (m + n) / 2 + 1); } else { return (findKth(A, m, B, n, (m + n) / 2) + findKth(A, m, B, n, (m + n) / 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) return B[k-1]; if(k == 1) return min(A[0], B[0]); int halfKA = min(k / 2, m), halfKB = k - halfKA; if(A[halfKA - 1] < B[halfKB - 1]) return findKth(A + halfKA, m - halfKA, B, n, k - halfKA); else if(A[halfKA - 1] > B[halfKB - 1]) return findKth(A, m, B + halfKB, n - halfKB, k - halfKB); else return A[halfKA - 1]; }};
Java版:
public class Solution { public double findMedianSortedArrays(int A[], int B[]) { int len = A.length + B.length; if(len % 2 == 0) return (findKth(A, B, len / 2) + findKth(A, B, len / 2 + 1)) / 2; else return findKth(A, B, len / 2 + 1); } public double findKth(int A[], int B[], int k) { if(A.length > B.length) return findKth(B, A, k); if(A.length == 0) return B[k-1]; if(k == 1) return Math.min(A[0], B[0]); int halfKA = Math.min(k/2, A.length); int halfKB = k - k / 2; if(A[halfKA - 1] < B[halfKB - 1]) { int temp[] = new int[A.length - halfKA]; System.arraycopy(A, halfKA, temp, 0, A.length - halfKA); return findKth(temp, B, k - halfKA); } else if(A[halfKA - 1] > B[halfKB - 1]) { int temp[] = new int[B.length - halfKB]; System.arraycopy(B, halfKB, temp, 0, B.length - halfKB); return findKth(A, temp, k - halfKB); } else return A[halfKA - 1]; }}
Python版:
class Solution: # @return a float def findMedianSortedArrays(self, A, B): length = len(A) + len(B) if length % 2 == 0: return (self.findKth(A, B, length / 2) + self.findKth(A, B, length / 2 + 1)) / 2; else: return self.findKth(A, B, length / 2 + 1); def findKth(self, A, B, k): if len(A) > len(B): return self.findKth(B, A, k) if len(A) == 0: return float(B[k-1]) if k == 1: return float(min(A[0], B[0])) halfKA = min(k / 2, len(A)) halfKB = k - k / 2 if A[halfKA - 1] < B[halfKB - 1]: return self.findKth(A[halfKA:], B, k - halfKA) elif A[halfKA - 1] > B[halfKB - 1]: return self.findKth(A, B[halfKB:], k - halfKB) else: return float(A[halfKA - 1])
- LeetCode 题解(66): Median of Two Sorted Arrays
- [LeetCode题解004]Median of Two Sorted Arrays
- leetcode题解||Median of Two Sorted Arrays问题
- [Leetcode题解]004 Median of Two Sorted Arrays
- LeetCode题解——4Median of Two Sorted Arrays
- LeetCode题解--4. Median of Two Sorted Arrays
- LeetCode题解:4. Median of Two Sorted Arrays
- Leetcode题解4.Median of Two Sorted Arrays
- LeetCode题解-4-Median of Two Sorted Arrays
- LeetCode题解–4. Median of Two Sorted Arrays
- leetcode题解Java | 4. Median of Two Sorted Arrays
- 4. Median of Two Sorted Arrays LeetCode题解
- LeetCode题解(4)--Median of Two Sorted Arrays
- leetcode题解-4. Median of Two Sorted Arrays
- LeetCode题解(Golang实现)--Median of Two Sorted Arrays
- LeetCode题解系列--4. Median of Two Sorted Arrays
- leetcode题解-4. Median of Two Sorted Arrays
- Leetcode题解(Python): 4.Median of Two Sorted Arrays (非递归)
- ios键盘收缩
- A/B 向上取整的方法
- Linq的分页
- mininet floodlight 搭建SDN环境
- javascript中函数做为参数的调用
- LeetCode 题解(66): Median of Two Sorted Arrays
- android--zxing返回扫描界面出现黑屏的解决方案
- Buffered读写中文文件乱码
- BZOJ1500 [NOI2005]维修数列
- 有人想将手中一张面值100元的人民币换成5元,1元,0.5元面值的零钱100张,以上三种面值的零钱至少有一张,总共有几种换法。
- .net中的抽象类与接口
- 寒假之作—HDU—卡片叠加问题
- putty工具来远程访问
- 【转发】关于Java性能的9个谬论