Leetcode 5——Median of Two Sorted Arrays
题目:
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(log (m+n)),所以不能用数组存。网上找的方法是比较两个数组中间的元素,如AB两个数组,如果A[mid]>B[mid],那中位数肯定就不在B的前半段,于是缩小了范围,即B后半段加上A,然后依次查找并缩小范围,直到找到中间的数为止。

public class Solution {
public double findMedianSortedArrays(int[] nums1, int[] nums2) {
int m=nums1.length;
int n=nums2.length;
if((m+n)%2==0)
return (findKMax((m+n)/2,nums1,0,nums2,0)+findKMax((m+n)/2+1,nums1,0,nums2,0))/2.0;//如果是偶数
else
return findKMax((m+n)/2+1,nums1,0,nums2,0);//如果是奇数
}
public int findKMax(int k,int[] nums1,int start1,int[] nums2,int start2){
if(start1>=nums1.length)//第一个数组长度为0的话,直接返回第二个数组的中位数
return nums2[start2+k-1];
if(start2>=nums2.length)//第二个数组长度为0的话,直接返回第一个数组的中位数
return nums1[start1+k-1];
if(k==1)//k=1,即找第一个,也就是nums1或nums2中最小的
return Math.min(nums1[start1], nums2[start2]);
int temp1=start1+k/2-1;
int temp2=start2+k/2-1;
int mid1=temp1<nums1.length?nums1[temp1]:Integer.MAX_VALUE;//nums1没有下标为temp1的元素,如果有,就用那个元素比较,没有就用最大int
int mid2=temp2<nums2.length?nums2[temp2]:Integer.MAX_VALUE;
if(mid1>=mid2)
return findKMax(k-k/2,nums1,start1,nums2,temp2+1);//如果mid1大,也就是nums1中间的数大,那么nums1前半段不会有中位数
else //从nums1后半段和nums1开始找
return findKMax(k-k/2,nums1,temp1+1,nums2,start2);
}
}
Leetcode 5——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 ...
随机推荐
- 芝麻HTTP:Python爬虫入门之正则表达式
1.了解正则表达式 正则表达式是对字符串操作的一种逻辑公式,就是用事先定义好的一些特定字符.及这些特定字符的组合,组成一个"规则字符串",这个"规则字符串"用来 ...
- js中的0就是false,非0就是true及案例
在处理js代码判断真假时经常会这么写. 但fun()可能得到的是数字0,这可不是表示的没有值,但是!js中的数字0就是false,非0就是true. 于是0就被无情的当做false了. 已经被这个坑过 ...
- Java8 日期/时间(Date Time)使用简介
特别说明: LocalDateTime 为日期时间的计算提供了很大的方便, 在构造对象/运算/toString等方便都非常便利. 3个常用的类: java.time.LocalDateTime; ja ...
- Python Cookbook(第3版)中文版:15.15 C字符串转换为Python字符串
15.15 C字符串转换为Python字符串¶ 问题¶ 怎样将C中的字符串转换为Python字节或一个字符串对象? 解决方案¶ C字符串使用一对 char * 和 int 来表示, 你需要决定字符串到 ...
- Labview中嵌入flex/flash
将flash动画作为ActiveX文档插入LabVIEW程序的前面板的方法 (1)在前面板插入Controls的ActiveX Container控件. (2)在Container控件上单击鼠标右键, ...
- 创建文本节点createTextNode
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...
- [Luogu2617]Dynamic Ranking
题面戳这 类似算法总结 1.静态整体Kth 滑稽吧...sort一遍就好了. 时间复杂度\(O(nlogn)\) 空间复杂度\(O(n)\) 2.动态整体Kth 离散化后开一棵权值线段树,每个位置的值 ...
- 【BZOJ4554】游戏(二分图匹配,网络流)
[BZOJ4554]游戏(二分图匹配,网络流) 题解 Description 在2016年,佳缘姐姐喜欢上了一款游戏,叫做泡泡堂.简单的说,这个游戏就是在一张地图上放上若干个炸弹,看 是否能炸到对手, ...
- IDEA的配置
一.使用配置 转自:http://blog.csdn.net/qq_27093465/article/details/52918873 setting: 设置外观和字体: 设置编辑器快捷键: 自动 ...
- kill 掉所有正在运行的hadoop jobs
# get list of job's process IDs JOB_LIST=$(hadoop job -list 2> /dev/null | grep job_ | awk '{prin ...