算法分析与设计——LeetCode Problem.4 Median of Two Sorted Arrays
来源:互联网 发布:淘宝网石头鱼大码女装 编辑:程序博客网 时间:2024/06/05 20:31
题目链接
问题详情
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(n),但题目要求的复杂度是O(log n)。然而我不理解的是这个方法的代码依然
通过了评测。代码如下:
class Solution {public:double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {vector<int> v;int p1 = 0, p2 = 0;while (p1 < nums1.size() && p2 < nums2.size()) {if (nums1[p1] < nums2[p2]) {v.push_back(nums1[p1]);p1++;}else {v.push_back(nums2[p2]);p2++;}}if (p1 == nums1.size()) {while (p2 < nums2.size()) {v.push_back(nums2[p2]);p2++;}}if (p2 == nums2.size()) {while (p1 < nums1.size()) {v.push_back(nums1[p1]);p1++;}}if (v.size() % 2 == 1) {return double(v[v.size() / 2]);}else {return double((v[v.size() / 2] + v[v.size() / 2 - 1] / 2.0));}}};
可以假设中位数是两个数组中第k小的数字,然后不断去掉两个数组中肯定不是中位数的部分,此时k的值随之变化,最后当k的值为1或者有一数组为空时即可找到
中位数。代码如下:
class Solution {public: double findMedian(vector<int> v1, vector<int> v2, int median) { //确保v1长度比v2小 if (v1.size() > v2.size()) { swap(v1, v2); } if (v1.size() == 0) { return v2[median - 1]; } int i1 = v1.size(); int i2 = v2.size(); if (median == 1) {//取两个数组中的最小值if (v1[0] < v2[0]) {return v1[0];}return v2[0]; } int t1 = min(i1, median / 2); int t2 = min(i2, median / 2); // if (v1[t1-1] > v2[t2-1]) { return findMedian(v1, vector<int> (v2.begin()+t2, v2.end()), median - t2); } else { return findMedian(vector<int> (v1.begin()+t1, v1.end()), v2, median - t1); } return 0; } double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) { //当nums1或nums2为空 if (nums2.size() == 0) { if (nums1.size() % 2 == 1) { return nums1[nums1.size() / 2]; } else { return double((nums1[nums1.size() / 2] + nums1[nums1.size() / 2 - 1]) / 2.0); } } if (nums1.size() == 0) { if (nums2.size() % 2 == 1) { return nums2[nums2.size() / 2]; } else { return double((nums2[nums2.size() / 2] + nums2[nums2.size() / 2 - 1]) / 2.0); } } int n1 = nums1.size(), n2 = nums2.size(); if ((n1 + n2) % 2 == 1) { return findMedian(nums1, nums2, (n1 + n2) / 2.0 + 1); } else { return (findMedian(nums1, nums2, (n1 + n2) / 2.0) + findMedian(nums1, nums2, (n1 + n2) / 2 + 1)) / 2.0; } }};
阅读全文
0 0
- 算法分析与设计——LeetCode Problem.4 Median of Two Sorted Arrays
- 算法分析与设计——LeetCode:4. Median of Two Sorted Arrays
- 算法分析与设计丨第三周丨LeetCode(5)——Median of Two Sorted Arrays(Hard)
- LeetCode — Median of Two Sorted Arrays
- LeetCode—Median of Two Sorted Arrays
- leetcode—Median of Two Sorted Arrays
- Leetcode problem 4:Median of Two Sorted Arrays
- LeetCode.Problem 4 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
- 【python】【leetcode】【算法题目4—Median of Two Sorted Arrays】
- LeetCode算法题4:Median of Two Sorted Arrays
- 算法设计与分析(3)-- Median of Two Sorted Arrays(难度:hard)
- 【算法分析与设计】【第二周】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
- ctf入门指南
- 多表连接查询
- chrome http请求,测试webapp接口之DHC
- 报错:[mysys_ssl/CMakeFiles/mysys_ssl.dir/my_default.cc.o] Error 1
- Python爬虫走代理
- 算法分析与设计——LeetCode Problem.4 Median of Two Sorted Arrays
- idea错误:main method not found in class
- 最长递增(递减)子序列
- 关于选择排序和快速排序--2017-09-24
- 数学专题小记
- 创建线程的两种方式以及区别
- JDK 9.0 新特性
- SpringMVC源码分析(一)从配置文件说起
- mac安装python3.6