leetcode Median of Two Sorted Arrays
来源:互联网 发布:我的世界网络错误 编辑:程序博客网 时间:2024/06/11 22:01
Description:
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
暴力求解可以先合并在找中值,时间复杂度是O(nlogn)。
另外的话,线性时间复杂度也很容易想到,用两个指针,分别指向nums1,nums2,然后移动,知道i+j = m。(m为中值)。
然后就是题目要求的log(m+n)算法了。这个还是比较难的,在看了很多博客之后,才找到一种比较容易明白的算法。
解决此题的方法可以依照:寻找一个unioned sorted array中的第k大(从1开始数)的数。因而等价于寻找并判断两个sorted array中第k/2(从1开始数)大的数。
特殊化到求median,那么对于奇数来说,就是求第(m+n)/2+1(从1开始数)大的数。
而对于偶数来说,就是求第(m+n)/2大(从1开始数)和第(m+n)/2+1大(从1开始数)的数的算术平均值。
那么如何判断两个有序数组A,B中第k大的数呢?
我们需要判断A[k/2-1]和B[k/2-1]的大小。
如果A[k/2-1]==B[k/2-1],那么这个数就是两个数组中第k大的数。
如果A[k/2-1]<B[k/2-1], 那么说明A[0]到A[k/2-1]都不可能是第k大的数,所以需要舍弃这一半,继续从A[k/2]到A[A.length-1]继续找。当然,因为这里舍弃了A[0]到A[k/2-1]这k/2个数,那么第k大也就变成了,第k-k/2个大的数了。
如果 A[k/2-1]>B[k/2-1],就做之前对称的操作就好。
这样整个问题就迎刃而解了。当然,边界条件页不能少,需要判断是否有一个数组长度为0,以及k==1时候的情况。
代码如下:
class Solution {public: double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) { int m = nums1.size(), n = nums2.size(); return (findKth(nums1, nums2, (m + n + 1) / 2) + findKth(nums1, nums2, (m + n + 2) / 2)) / 2.0; } int findKth(vector<int> nums1, vector<int> nums2, int k) { int m = nums1.size(), n = nums2.size(); if (m > n) return findKth(nums2, nums1, k); if (m == 0) return nums2[k - 1]; if (k == 1) return min(nums1[0], nums2[0]); int i = min(m, k / 2), j = min(n, k / 2); if (nums1[i - 1] > nums2[j - 1]) { return findKth(nums1, vector<int>(nums2.begin() + j, nums2.end()), k - j); } else { return findKth(vector<int>(nums1.begin() + i, nums1.end()), nums2, k - i); } return 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
- 东北大学科学家利用CNN识别贫血患者红细胞
- 创建一个带有SQLserver数据库模型的MVC项目
- 世界最大色情网站Pornhub使用AI识别AV,鉴黄师要下岗了!
- 阿联酋任命第一位人工智能部长:谢赫穆罕默德·本·拉希德·阿勒马克图姆
- opencv通过鼠标获取训练图像
- leetcode Median of Two Sorted Arrays
- C++笔记——标准模板库STL
- github
- 欢迎使用CSDN-markdown编辑器
- C++风格_头文件
- 23种设计模式
- GreenDao3.2的基本使用入门
- Android电商项目 ExpandableListView(二级列表)实现购物车(高仿淘宝) demo
- java常用设计模式(适配模式)