Leetcode题解 - 4. Median of Two Sorted Arrays
2017-04-10 11:33
417 查看
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
链接
1)如果A或者B为空,直接返回B[k-1]或者A[k-1];
2)如果k为1,返回A[0]和B[0]中较小值;
3)如果A[k/2-1]等于B[k/2-1],返回其中一个值即可;
4)如果A[k/2-1]小于B[k/2-1],则查找范围去除A[0]到A[k/2-1]数据,同时更新k的值继续递归计算;
5)如果A[k/2-1]大于B[k/2-1],则查找范围去除B[0]到B[k/2-1]数据,同时更新k的值继续递归计算即可。
ref:
http://blog.csdn.net/yzhang6_10/article/details/51010058
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
链接
解一
归并排序的方法,把两个数组归并排序然后找其中位数class Solution { public: double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) { vector<int> merged; int i=0,j=0; while(i<nums1.size()&&j<nums2.size()){ if(nums1[i]>=nums2[j]) merged.push_back(nums2[j++]); else merged.push_back(nums1[i++]); } while(i<nums1.size()){ merged.push_back(nums1[i++]); } while(j<nums2.size()){ merged.push_back(nums2[j++]); } if(merged.size()%2==0){ int idx=(0+merged.size()-1)/2; double tmp=(double(merged[idx])+double(merged[idx+1]))/2; return tmp; } else return merged[(0+merged.size()-1)/2]; } };
解二
递归实现1)如果A或者B为空,直接返回B[k-1]或者A[k-1];
2)如果k为1,返回A[0]和B[0]中较小值;
3)如果A[k/2-1]等于B[k/2-1],返回其中一个值即可;
4)如果A[k/2-1]小于B[k/2-1],则查找范围去除A[0]到A[k/2-1]数据,同时更新k的值继续递归计算;
5)如果A[k/2-1]大于B[k/2-1],则查找范围去除B[0]到B[k/2-1]数据,同时更新k的值继续递归计算即可。
class Solution { public: double dfs(vector<int> &nums1, int i, vector<int> &nums2, int j, int k) { // 当nums1为空的情况 if (nums1.size() == i) return nums2[j + k - 1]; // 当nums2为空的情况 if (nums2.size() == j) return nums1[i + k - 1]; // 当k = 1的情况 if (k == 1) return min(nums1[i], nums2[j]); // 计算nums1和nums2的待查找长度 int len1 = nums1.size() - i; int len2 = nums2.size() - j; // 如果nums1待查找长度大于nums2待查找长度,交换 if (len1 > len2) return dfs(nums2, j, nums1, i, k); // 计算nums1和nums2待查找长度的中间值位置 int temp = nums1.size(); int mid1 = min(i + k / 2, temp); int mid2 = j + k - (mid1 - i); // 判断nums1和nums2待查找长度中间值大小 // 当nums1待查找的中间值小于nums2待查找的中间值,则移除nums1的前一半数据,进行递归查找 if (nums1[mid1 - 1] < nums2[mid2 - 1]) return dfs(nums1, mid1, nums2, j, k - mid1 + i); // 当nums2待查找的中间值小于nums1待查找的中间值,则移除nums2的前一半数据,进行递归查找 else if (nums1[mid1 - 1] > nums2[mid2 - 1]) return dfs(nums1, i, nums2, mid2, k - mid2 + j); // 当nums1和nums2待查找的中间值相等,则直接返回其中的一个中间值即为结果 else return nums1[mid1 - 1]; } double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) { int m=nums1.size(); int n=nums2.size(); int total = m + n; // 当待查数据为奇数,返回中间值 if (total % 2 == 1) return dfs(nums1, 0, nums2, 0, total / 2 + 1); // 当待查数据为偶数,返回两个中间值的和的一半 else return (dfs(nums1, 0, nums2, 0, total / 2) + dfs(nums1, 0, nums2, 0, total / 2 + 1)) / 2; } };
ref:
http://blog.csdn.net/yzhang6_10/article/details/51010058
相关文章推荐
- [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题解004]Median of Two Sorted Arrays
- LeetCode题解——Median of Two Sorted Arrays
- LeetCode题解系列--4. Median of Two Sorted Arrays
- LeetCode题解--4. Median of Two Sorted Arrays
- [LeetCode]题解(python):004-Median of Two Sorted Arrays
- LeetCode题解-4-Median of Two Sorted Arrays
- LeetCode题解(Golang实现)--Median of Two Sorted Arrays
- LeetCode: 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 26: Median of Two Sorted Arrays
- [leetcode]Median of Two Sorted Arrays
- LeetCode: Median of Two Sorted Arrays
- LeetCode刷题笔录 Median Of Two Sorted Arrays