【LeetCode】4. Median of Two Sorted Arrays(思维)
【题意】
给两个有序数组,寻找两个数组组成后的中位数,要求时间复杂度为O(log(n+m))。
【题解】
感觉这道题想法非常妙!!
假定原数组为a,b,数组长度为lena,lenb。
那么中位数一定是第k = (lena + lenb + 1)/ 2小的数,如果是数组长度和是偶数的话就是第k = (lena + lenb + 1)/ 2小和第k = (lena + lenb + 2)/ 2小的数,所以我们可以把问题转化为求第k小的数。
然后分别对a,b找第k / 2小的数,假如a[k / 2 - 1]和b[k / 2 -1] ,如果a[k / 2 - 1 > b[k / 2 - 1],那么就说明b[k / 2 - 1]必然不是我们要找到的答案,也就是说k / 2 - 1之前的数字我们都可以排除掉,下一次b数组的开始位置变成k / 2,然后下一次就要寻找第k = k - (k / 2)小的数(此处默认从0开始)了,然后不断递归,直到k = 1,也就是说当前指向的数就是我们要找的数,这时只要取两者最小值即可。
【代码】

1 class Solution {
2 public:
3 double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
4 int len1 = nums1.size();
5 int len2 = nums2.size();
6 int k1 = (len1 + len2 + 1) / 2;
7 int k2 = (len1 + len2 + 2) / 2;
8 double ans = (findk(0, len1 - 1, nums1, 0, len2 - 1, nums2, k1) * 1.0 + findk(0, len1 - 1, nums1, 0, len2 - 1, nums2, k2) * 1.0) / 2;
9 return ans;
10 }
11 int findk(int st1, int en1, vector<int>& nums1, int st2, int en2, vector<int>& nums2, int k)
12 {
13 int len1 = en1 - st1 + 1;
14 int len2 = en2 - st2 + 1;
15 // 使nums1的长度保持小于nums2
16 if (len1 > len2)
17 return findk(st2, en2, nums2, st1, en1, nums1, k);
18 if (len1 == 0)return nums2[st2 + k - 1];
19 if (k == 1)return min(nums1[st1], nums2[st2]);
20
21 int i = st1 + min(len1, k / 2) - 1;
22 int j = st2 + min(len2, k / 2) - 1;
23 if (nums1[i] > nums2[j])
24 return findk(st1, en1, nums1, j + 1, en2, nums2, k - (j - st2 + 1));
25 else
26 return findk(i + 1, en1, nums1, st2, en2, nums2, k - (i - st1 + 1));
27 }
28 };
【LeetCode】4. Median of Two Sorted Arrays(思维)的更多相关文章
- 【算法之美】求解两个有序数组的中位数 — leetcode 4. Median of Two Sorted Arrays
一道非常经典的题目,Median of Two Sorted Arrays.(PS:leetcode 我已经做了 190 道,欢迎围观全部题解 https://github.com/hanzichi/ ...
- LeetCode(3) || Median of Two Sorted Arrays
LeetCode(3) || Median of Two Sorted Arrays 题记 之前做了3题,感觉难度一般,没想到突然来了这道比较难的,星期六花了一天的时间才做完,可见以前基础太差了. 题 ...
- LeetCode 4 Median of Two Sorted Arrays (两个数组的mid值)
题目来源:https://leetcode.com/problems/median-of-two-sorted-arrays/ There are two sorted arrays nums1 an ...
- Leetcode 4. Median of Two Sorted Arrays(二分)
4. Median of Two Sorted Arrays 题目链接:https://leetcode.com/problems/median-of-two-sorted-arrays/ Descr ...
- LeetCode 4. Median of Two Sorted Arrays & 归并排序
Median of Two Sorted Arrays 搜索时间复杂度的时候,看到归并排序比较适合这个题目.中位数直接取即可,所以重点是排序. 再来看看治阶段,我们需要将两个已经有序的子序列合并成一个 ...
- 第三周 Leetcode 4. Median of Two Sorted Arrays (HARD)
4. Median of Two Sorted Arrays 给定两个有序的整数序列.求中位数,要求复杂度为对数级别. 通常的思路,我们二分搜索中位数,对某个序列里的某个数 我们可以在对数时间内通过二 ...
- Leetcode 4. Median of Two Sorted Arrays(中位数+二分答案+递归)
4. Median of Two Sorted Arrays Hard There are two sorted arrays nums1 and nums2 of size m and n resp ...
- LeetCode 004 Median of Two Sorted Arrays
题目描述:Median of Two Sorted Arrays There are two sorted arrays A and B of size m and n respectively. F ...
- leetcode 4. Median of Two Sorted Arrays
https://leetcode.com/problems/median-of-two-sorted-arrays/ There are two sorted arrays nums1 and num ...
- leetcode之 median of two sorted arrays
这是我做的第二个leetcode题目,一开始以为和第一个一样很简单,但是做的过程中才发现这个题目非常难,给人一种“刚上战场就踩上地雷挂掉了”的感觉.后来搜了一下leetcode的难度分布表(leetc ...
随机推荐
- mybatis(六)插件机制及分页插件原理
转载:https://www.cnblogs.com/wuzhenzhao/p/11120848.html MyBatis 通过提供插件机制,让我们可以根据自己的需要去增强MyBatis 的功能.需要 ...
- POJ3233 构造子矩阵+矩阵快速幂
题意:给你矩阵A,求S=A+A^1+A^2+...+A^n sol:直接把每一项解出来显然是不行的,也没必要. 我们可以YY一个矩阵: 其中1表示单位矩阵 然后容易得到: 可以看出这个分块矩阵的左下角 ...
- Dapr 正式发布1.0
年前我写了一篇博客<Dapr 已在塔架就位 将发射新一代微服务>, 今天Dapr 正式发布了1.0 : Dapr Runtime v1.0.0 Dapr dotnet SDK v1.0.0 ...
- js with All In One
js with All In One 不推荐,要废弃 function f(x, o) { with (o) { console.log(x); } } function f(foo, values) ...
- 前端 vs 后端
前端 vs 后端 前端与后端: 有什么区别? 前端和后端是计算机行业中最常用的两个术语. 在某种程度上,它们成了流行语. 它们决定了您作为软件开发人员所从事的工作类型,所使用的技术以及所获得的收入. ...
- window.ShadyCSS
window.ShadyCSS Web Components # install $ yarn add @webcomponents/shadycss@1.7.1 # OR $ npm i @webc ...
- TypeScript with React
TypeScript with React # Make a new directory $ mkdir react-typescript # Change to this directory wit ...
- Raspberry Pi & GPIO
Raspberry Pi & GPIO pinout === pin out / p in out pi@raspberrypi:~ $ pinout ,------------------- ...
- NGK流动性挖矿为何会备受瞩目?
随着越来越多资金的涌入,参与DeFi项目或挖矿的用户不难发现,使用体验不尽人意,在以太坊网络的DeFi时常需要漫长的等待确认和高昂的GAS费用,加上DeFi流动性挖矿需要相对较高的资金门槛和技术门槛, ...
- c# winform中窗体切换后释放及防止重复生成
问题1:窗体切换后如何关闭,并释放资? c# winform中,2个窗体,form1和form2,互相切换的时候执行 this.Hide(); Form2 form2 = new Form2(); f ...