**Leetcode_median-of-two-sorted-arrays (c++ and python version)
来源:互联网 发布:送货单打印软件 编辑:程序博客网 时间:2024/06/05 02:04
地址:http://oj.leetcode.com/problems/median-of-two-sorted-arrays/
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)).
思路:这题参考了github上一份代码,注意时间复杂度要求是O(log (m+n)),看到这个应该联想到是系统递归的栈消耗。
find_kth_num(第K小数)使用于在两个array里找到第K小的数,不仅仅是中值。
另外注意k和下标索引差1.
这道题刚开始做比较难理解,需要多揣摩。
c++ 参考代码;
class Solution {public: //find kth smallest number, which applies to any kth number double find_kth_num(int A[], int m, int B[], int n, int k) { if(m > n) return find_kth_num(B, n, A, m, k); if(m==0) return B[k-1]; if(k == 1) return min(A[0], B[0]); int idx_a = min(m, k/2); int idx_b = k - idx_a; if(A[idx_a-1] < B[idx_b-1]) return find_kth_num(A+idx_a, m-idx_a, B, n, k-idx_a); else if(A[idx_a-1] > B[idx_b-1]) return find_kth_num(A, m, B+idx_b, n-idx_b, k-idx_b); else //these two parts together count to lenght k, just find the kth smallest number return A[idx_a-1]; } double findMedianSortedArrays(int A[], int m, int B[], int n) { if((m+n)&1) return (double)find_kth_num(A, m, B, n, (m+n)/2+1); else return 0.5 * (find_kth_num(A, m, B, n, (m+n)/2) + find_kth_num(A, m, B, n, (m+n)/2+1)); }};
python 参考代码:
class Solution: # @return a float def find_kth_num(self, A, m, B, n, k): if m > n: return self.find_kth_num(B, n, A, m, k) if not A: return float(B[k-1]) if k == 1: return float(min(A[0], B[0])) idx_a = min(m, k//2) idx_b = k - idx_a if A[idx_a-1] < B[idx_b-1]: return self.find_kth_num(A[idx_a:], m-idx_a, B, n, k-idx_a) elif A[idx_a-1] > B[idx_b-1]: return self.find_kth_num(A, m, B[idx_b:], n-idx_b, k-idx_b) else: return float(A[idx_a-1]) def findMedianSortedArrays(self, A, B): if (len(A)+len(B)) & 0x1: return self.find_kth_num(A, len(A), B, len(B), (len(A)+len(B)+1)//2) else: return (self.find_kth_num(A, len(A), B, len(B), (len(A)+len(B))//2) + self.find_kth_num(A, len(A), B, len(B), (len(A)+len(B))//2 + 1)) / 2.0
c++
Another trial, more simpler
class Solution {private: double find(int A[], int m, int B[], int n, int k) { if(m > n) return find(B, n, A, m, k); if(!m) return B[k-1]; if(k==1) return min(A[0], B[0]); int x = min(k/2, m); if(A[x-1] < B[x-1]) return find(A+x, m-x, B, n, k-x); else return find(A, m, B+x, n-x, k-x); }public: double findMedianSortedArrays(int A[], int m, int B[], int n) { if((m+n)%2) return find(A, m, B, n, (m+n)/2+1); else return 0.5*(find(A, m, B, n, (m+n)/2) + find(A, m, B, n, (m+n)/2+1)); }};
0 0
- **Leetcode_median-of-two-sorted-arrays (c++ and python version)
- 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]2 Median of Two Sorted Arrays (C++,Python实现)
- 【leetcode】4. Median of Two Sorted Arrays(Python & C++)
- Median of Two Sorted Arrays -- leetcode -- Python
- leetcode04-Median of Two Sorted Arrays-python
- 4. Median of Two Sorted Arrays-python
- 【Leetcode】【python】Median of Two Sorted Arrays
- 004Median of Two Sorted Arrays (C)
- leetcode Median of Two Sorted Arrays (C)
- 4. Median of Two Sorted Arrays(C++)
- [LeetCode] 004. Median of Two Sorted Arrays (Hard) (C++/Java/Python)
- 【算法】【Divide and conquer】Median of Two Sorted Arrays
- 深入理解JavaScript系列(32):设计模式之观察者模式
- class struct
- 深入理解JavaScript系列(34):设计模式之命令模式
- 线性表-----单链表
- 深入理解JavaScript系列(35):设计模式之迭代器模式
- **Leetcode_median-of-two-sorted-arrays (c++ and python version)
- JavaScript实现的购物车效果-好友列表效果
- 详细了解SQLITE 优缺点 性能测试
- 深入理解JavaScript系列(36):设计模式之中介者模式
- Android画图Path的使用
- 【中断异常】X86对中断的支持原理
- Eclipse开发工具知识必备
- Eclipse 集成JavaScript工具----spket
- 深入理解JavaScript系列(37):设计模式之享元模式