【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 ...
随机推荐
- Petrozavodsk Summer Training Camp 2016H(多标记线段树)题解
题意: \(n\)个草,第\(0\)天种下,高度都为\(0\),每个草每天长高\(a_i\).现给出\(q\)询问,每次给出第\(b_i\)天,然后把高于\(d_i\)的全削成\(d_i\),每次问你 ...
- ysoserial Commons Collections1反序列化研究
Apache Commons Collections1反序列化研究 环境准备 Apache Commons Collections 3.1版本 IDEA 需要一些java基础,反射.类对象.Class ...
- CSS3 动态生成内容(在Web中插入内容)====CSS的伪类或者伪元素
# css3 .类:伪类::伪元素 /* CSS3伪元素/伪类 :https://www.w3.org/TR/css3-selectors/#selectors ::selection 伪元素(F12 ...
- js 深入原理讲解系列-currying function
js 深入原理讲解系列-currying function 能看懂这一题你就掌握了 js 科里函数的核心原理 不要专业的术语,说人话,讲明白! Q: 实现 sum 函数使得以下表达式的值正确 cons ...
- CSS style color all in one
CSS style color all in one https://developer.mozilla.org/en-US/docs/Web/CSS/color_value /* Hexadecim ...
- Vue 组件之间通信 All in One
Vue 组件之间通信 All in One 组件间通信 1. 父子组件之间通信 https://stackblitz.com/edit/vue-parent-child-commutation?fil ...
- taro taro 多端同步调试
taro 多端同步调试 debug https://nervjs.github.io/taro/docs/envs-debug.html
- Chrome DevTools & console & filter warning
Chrome DevTools & console & filter warning
- React & Desktop App
React & Desktop App https://proton-native.js.org/#/ https://github.com/kusti8/proton-native
- SPC空投糖果,是白捡还是风险?
2020年,币圈刮起了空投风,很多项目纷纷"撒钱"在空投中,在空投中获利多者白捡上百万美刀,少的也薅了万把块羊毛,币圈的空投无时不刻透露着天上掉馅饼的气息.NGK官方在2020年年 ...