leetcode4 Median of Two Sorted Arrays学习记录
学习了扁扁熊的题解:https://leetcode-cn.com/problems/median-of-two-sorted-arrays/solution/4-xun-zhao-liang-ge-you-xu-shu-zu-de-zhong-wei-shu/
记录一下,顺便按自己的理解给代码加上注释
#include <vector>
#include <stdio.h>
using namespace std;
#define max(a,b) (((a)>(b)) ? (a) : (b) )
#define min(a,b) (((a)<(b)) ? (a) : (b) )
class Solution {
public:
double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
int m=nums1.size();
int n=nums2.size();
//保持数组1为短的是为了提高效率,其实只要选定一个数组二分就可以了。
if(m>n)
{
return findMedianSortedArrays(nums2,nums1);
}
//lo是数组的左部,hi是数组的末尾,为了防止奇偶问题,把数组长度已经变成2m+1和2n+1
//数组末尾应该是2m+1,下标从0开始,所以hi是2m
int LMax1,LMax2,RMin1,RMin2,c1,c2,lo=0,hi=m*2;
//开始二分数组1
while(lo<=hi)
{
c1=(lo+hi) /2 ;
c2 = m+n - c1; //因为分别数组扩大到了2倍,所以是中位数在m+n+1 即(2m+1+2n+1)/2
//我们知道要保持从c1+c2=k,注意m+n就是相当于m+n+1(下标从0开始)
//当数组1二分完了,如果遇到割在数组的两边,说明整个数组都是大于或者小于目标中位数的
//这个时候数组1的LMax和RMin是不能参与计算最后的中位数
//所以要特殊处理一下,例如c1==0,说明数组1整个都大于目标中位数,那么LMax1一定会大于LMax2
//可我们不希望LMax1作为结果的一部分,结果应该是由LMax2和RMin2计算出来的,所以此时强行把LMax1=INT_MIN
//这样最后求中位数,就Max(LMax1,LMax2)就不会返回LMax1而是LMAx2。别的情况以此类推
LMax1=(c1==0)?INT_MIN:nums1[(c1-1)/2];
RMin1=(c1==2*m)?INT_MAX:nums1[c1/2];
LMax2=(c2==0)?INT_MIN:nums2[(c2-1)/2];
RMin2=(c2==2*n)?INT_MAX:nums2[c2/2];
if(LMax1>RMin2)
{
hi=c1-1;
}else if(LMax2>RMin1)
{
lo=c1+1;
}else
break;
}
//题解里解释过,拿#填充扩大2倍的数组,中位数=(LMax+RMin)/2
return (max(LMax1,LMax2)+min(RMin1,RMin2))/2.0;
}
};
主要是记录一下,学习完扁扁熊题解的感悟,顺便写一下大佬没详细说明的部分。
leetcode4 Median of Two Sorted Arrays学习记录的更多相关文章
- Leetcode4:Median of Two Sorted Arrays@Python
There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...
- LeetCode4 Median of Two Sorted Arrays
题目: There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the ...
- Leetcode4.Median of Two Sorted Arrays两个排序数组的中位数
给定两个大小为 m 和 n 的有序数组 nums1 和 nums2 . 请找出这两个有序数组的中位数.要求算法的时间复杂度为 O(log (m+n)) . 你可以假设 nums1 和 nums2 不同 ...
- 【转载】两个排序数组的中位数 / 第K大元素(Median of Two Sorted Arrays)
转自 http://blog.csdn.net/zxzxy1988/article/details/8587244 给定两个已经排序好的数组(可能为空),找到两者所有元素中第k大的元素.另外一种更加具 ...
- 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第四题:Median of Two Sorted Arrays (java)
Median of Two Sorted Arrays There are two sorted arrays A and B of size m and n respectively. Find t ...
- 4. Median of Two Sorted Arrays(topK-logk)
4. Median of Two Sorted Arrays 题目 There are two sorted arrays nums1 and nums2 of size m and n respec ...
- 【算法之美】求解两个有序数组的中位数 — leetcode 4. Median of Two Sorted Arrays
一道非常经典的题目,Median of Two Sorted Arrays.(PS:leetcode 我已经做了 190 道,欢迎围观全部题解 https://github.com/hanzichi/ ...
- [LintCode] Median of Two Sorted Arrays 两个有序数组的中位数
There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted ...
随机推荐
- Linux学习笔记-第7天 - 编程还是要多写多练
编程思路很重要,多写是要熟悉命令用法,多练不只是要熟悉语句常用在什么环境,更要在其基础上,尝试更多的写法.
- 5-ESP8266 SDK开发基础入门篇--了解一下操作系统
对于操作系统不知道有没有害怕接触的... 先说一下操作系统是什么意思,其实咱的电脑就运行了操作系统,手机,等等... 操作系统和任务分不开,所谓任务就是一个一个的执行各个功能的函数,,,操作系统呢就是 ...
- 初赛Part2
初赛 时间复杂度 主定理(必考) \[ T(n) = aT(\frac{n}{b})+f(n) \] 其中,\(n\)为问题的规模,\(a\)为递推下子问题的数量,\(\frac{n}{b}\)为每个 ...
- P3613 【深基15.例2】寄包柜
传送门 题目大意 往一个\(a[i][j]\) 里边放东西,也可以取走东西,然后查询\(a[i][j]\)里边是什么东西. 思路: 显然我们可以暴力,但是你开不了那么大的数组. 翻了翻dalao们的题 ...
- zr2019暑期高端峰会AB组十测
郑睿round 1 代码真的好写,还是太笨了,爆零了. 郑睿round_2 好失败,A题真的是日狗了,第一发就可以A的,忘记费用流的反向边也要加一发流量了.哎,我注定是要爆零. 正睿round_3 日 ...
- PKUSC2018题解
PKUSC2018题解 真实排名 分别考虑第\(i\)个人翻倍和不翻倍的情况,组合数算一下即可,务必注意实现细节. 代码 最大前缀和 设\(sum_s\)表示集合\(\sum_{i\in s} a_i ...
- A1050 String Subtraction (20 分)
一.技术总结 这个是使用了一个bool类型的数组来判断该字符是否应该被输出. 然后就是如果在str2中出现那么就判断为false,被消除不被输出. 遍历str1如果字符位true则输出该字符. 还有需 ...
- find sum and average of n numbers
public class Solution { public static void main(String[] args) { Scanner ip = new Scanner(System.in) ...
- Spring mvc 前后台通过json交互【转】
原文转自:https://www.cnblogs.com/zhaojiankai/p/8184596.html 本节内容: @RequestBody @ResponseBody 请求json,响应js ...
- JDK Mac 安装
JDK安装步骤 一台mac os 环境 jdk.sdk.Android studio 1.打开终端-输入命令Java 2.从下面的图中可以看到,终端会自动给出提示,没有可以使用的java命令 ...