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 ...
随机推荐
- python的可变类型和不可变类型
Python有六种数据类型:数字类型.字符串类型.列表类型.元组类型.字典类型和集合类型 其中不可变类型包括三种:数字类型.字符串类型和元组类型 剩余三种为可变类型:列表类型.字典类型和集合类型 可变 ...
- MacbookPro升级10.15 Catalina之后无法读写NTFS
冲着Sidecar的双屏功能,乐呵呵的跑去升级了10.15,结果就悲剧了. 所有移动硬盘和U盘都写不了,无奈只好上网找办法,目前找到一个便宜的方法: 共2步: Step 1:编写fstab文件 使用T ...
- 第02组 Beta冲刺(4/4)
队名:十一个憨批 组长博客 作业博客 组长黄智 过去两天完成的任务:了解整个游戏的流程 GitHub签入记录 接下来的计划:继续完成游戏 还剩下哪些任务:完成游戏 燃尽图 遇到的困难:没有美术比较好的 ...
- [LeetCode] 394. Decode String 解码字符串
Given an encoded string, return it's decoded string. The encoding rule is: k[encoded_string], where ...
- [LeetCode] 198. House Robber 打家劫舍
You are a professional robber planning to rob houses along a street. Each house has a certain amount ...
- [LeetCode] 41. First Missing Positive 首个缺失的正数
Given an unsorted integer array, find the smallest missing positive integer. Example 1: Input: [1,2, ...
- 源码解读SLF4J绑定日志实现的原理
一.导读 我们使用log4j框架时,经常会用slf4j-api.在运行时,经常会遇到如下的错误提示: SLF4J: Class path contains multiple SLF4J binding ...
- plsql安装
1.plsql都需要安装oracle的客户端,不过也可以安装瘦客户端,完整的客户端太大了,俗称胖客户端,一般瘦客户端就可以满足, 本人下载的是instantclient-basic-win32-11. ...
- oracle--数据库扩容后出现ORA-27102
一,问题描述 Connected to an idle instance. SQL> startup nomount ORA: obsolete or deprecated parameter( ...
- 日志模块log4js的配置说明
1:先安装log4js模块 npm install log4js 目前安装的版本信息:"log4js": "^3.0.5" 2:引用及配置 var log4js ...