Median of Two Sorted Arrays--LeetCode
来源:互联网 发布:用php编写一个表格 编辑:程序博客网 时间:2024/06/04 01:25
1.题目
Median of Two Sorted Arrays
There are two sorted arrays nums1 and nums2 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)).
Example 1:
nums1 = [1, 3]
nums2 = [2]
The median is 2.0
Example 2:
nums1 = [1, 2]
nums2 = [3, 4]
The median is (2 + 3)/2 = 2.5
2.题意
查找两个有序数组的中值,时间复杂度为O(log (m+n))
3.分析
1)类似Merge Sorted Array,先把两个有序数组合并
当合并到第len/2个元素的时候返回那个数即可
不需要保存结果数组
算法时间复杂度是O(m+n),空间复杂度是O(1)
2)借助算法导论10.3节中的order statistics思想
问题等价于求两数组中第k=len/2大的数
基本思路是每次通过查看两数组的第k/2大的数(假设是A[k/2],B[k/2])
如果A[k/2]=B[k/2],说明A[k/2]和B[k/2]即为两个数组剩余元素的第k大的数
如果A[k/2]>B[k/2], 那么说明B的前k/2中不存在我们要的第k大的数
反之则去除A的前k/2,这样每次排除k/2个元素,最终k=1时即为结果
总的时间复杂度是O(logk),空间复杂度也是O(logk),即递归栈的大小
因为k=(m+n)/2,所以复杂度是O(log(m+n))
注意传参时候为find(A, B, i + posA, i2, j, j + posB - 1, k - posA);
不要写成find(A, B, i + posA, i2, j, j + posB - 1, k);
4.代码
class Solution {public: double findMedianSortedArrays(vector<int>& A, vector<int>& B) { int lenA = A.size(); int lenB = B.size(); int len = lenA + lenB; if(len & 0x1) return find(A, B, 0, lenA - 1, 0, lenB - 1, len / 2 + 1); else return (find(A, B, 0, lenA - 1, 0, lenB - 1, len / 2) + find(A, B, 0, lenA - 1, 0, lenB - 1, len / 2 + 1)) / 2.0; }private: int find (vector<int>& A, vector<int>& B, int i, int i2, int j, int j2, int k) { int m = i2 - i + 1; int n = j2 - j + 1; if(m > n) return find(B, A, j, j2, i, i2, k); if(m == 0) return B[j + k - 1]; if(k == 1) return min(A[i], B[j]); int posA = min(k / 2, m); int posB = k - posA; if(A[i + posA - 1] == B[j + posB - 1]) return A[i + posA -1]; else if(A[i + posA - 1] < B[j + posB - 1]) return find(A, B, i + posA, i2, j, j + posB - 1, k - posA); else return find(A, B, i, i + posA - 1, j + posB, j2, k - posB); }};
- 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
- 网易面试汇总
- Permission Denial: reading com.android.providers.media.MediaProvider
- 数据结构:图的存储结构之邻接矩阵
- Tensorflow基础:卷积神经网络迁移学习
- 使用BigDecimal进行精确运算
- Median of Two Sorted Arrays--LeetCode
- AS-->一键依赖全部或多个aar文件
- Matplotlib--legend
- 解决Ubuntu的root账号无法登录SSH问题-Permission denied, please try again.
- 云客Drupal8源码分析之字段API(上)
- 有没有人遇到过gcc编译程序正常运行,g++编译就传递参数出现问题
- Verilog HDL我遇到的错误经验
- poj1007 DNA Sorting
- 面向GNU/Linux桌面环境的KDE Plasma 5.11正式发布