leetCode之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
【解题思路】
1、整体排序,去中间值,但是,实际上,我们不需要完整的排序完成,只需要排序middle左边的即可,这样就能保证middle所在的位置是中间位置。
2、需要考虑num1和mun2全部长度为偶数和奇数的问题,因为如果是奇数,中间值为一个,如果是偶数,中间值为两个。这个其实,终归是要在两个数组中查找一个具体的值。
3、当一个数组已经遍历完了,但没有找到值时,要在另外一个数组中继续遍历。
【代码实现】
public double findMedianSortedArrays(int[] nums1, int[] nums2) {
int n1 = nums1.length;
int n2 = nums2.length;
int total = n1 + n2;
if(total % 2 == 1)//奇数情况
{
return findMiddle(nums1, nums2, n1, n2, (total/2 + 1));
}
else
{
return (findMiddle(nums1, nums2, n1, n2, (total/2)) + findMiddle(nums1, nums2, n1, n2, (total/2 + 1)))/2;
}
}
private double findMiddle(int[] nums1, int[] nums2, int n1, int n2, int midIndex)
{
double middle = 0.0;
int i = 0, j = 0;
for(; i<n1 && j<n2; )
{
if(nums1[i] < nums2[j])
{
i++;
middle = nums1[i-1];
}
else
{
j++;
middle = nums2[j-1];
}
if((i+j) == midIndex)
{
break;
}
}
while(i<n1 && ((i+j) < midIndex))
{
i++;
middle = nums1[i-1];
if((i+j) == midIndex)
{
break;
}
}
while(j<n2 && ((i+j) < midIndex))
{
j++;
middle = nums2[j-1];
if((i+j) == midIndex)
{
break;
}
}
return middle;
}
【后续】
发现了更好的方法,通过分治来实现,方法确实巧妙。
可参考链接:
https://hk029.gitbooks.io/leetbook/%E5%88%86%E6%B2%BB/004.%20Median%20of%20Two%20Sorted%20Arrays[H]/004.%20Median%20of%20Two%20Sorted%20Arrays[H].html
leetCode之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 ...
随机推荐
- iOS 大批量弹幕小论(粒子弹幕)
一.现状 如今直播类.视频播放器等基本都有弹幕模式. 为了保持性能和内存可控,基本是在初始化的时候生成一个Pool(Pool的容量是设定好的), 也就是利用重用机制(可以想象一下UITableView ...
- RenderScript多输入参数
https://stackoverflow.com/questions/20783830/how-to-use-renderscript-with-multiple-input-allocations ...
- OpenStack、KVM、VMWare和Docker
一.虚拟化 1.什么是虚拟化 虚拟化,是指通过虚拟化技术将一台计算机虚拟为多台逻辑计算机.在一台计算机上同时运行多个逻辑计算机,每个逻辑计算机可运行不同的操作系统,并且应用程序都可以在相互独立的空间内 ...
- go channel 阻塞
初接触 go 有不对得请解释下 Channel是goroutine之间进行通信的一种方式,先看下面代码为什么会阻塞: func closer(ch chan int) { ch <- 1 log ...
- vROPS中获取虚拟机在VC中的UUID
vROPS中虚拟机对象的ID为resourceID,跟vCenter中虚拟机的UUID是不一致的,因此想要将vROPS中的虚拟机和vCenter中的虚拟机对应肯定不能靠虚拟机名称,而是一定要靠UUID ...
- ThreadLocal 从源码角度简单分析
目录 ThreadLcoal源码浅析 ThreadLocal的垃圾回收 Java引用 ThreadLocal的回收 各线程中threadLocalMap的回收 内存泄露问题 总结 参考 ThreadL ...
- jquery post 同步异步总结[转]
1.post被请求多次,解决方法: 连接加入随机数 rand=""+Math.random() $.post("/Control/webControl.ashx?rand ...
- linux 卸载jdk和安装
卸载JDK 1.先输入java -version 查看是否安装了jdk 2.如果安装了,检查下安装的路径 which java(查看JDK的安装路径) 3.卸载 rm -rf JDK地址(卸载JDK) ...
- LeetCode——3Sum Closest
Question Given an array S of n integers, find three integers in S such that the sum is closest to a ...
- Ad Exchange基本接口和功能
这里描述下一个exchange通常应该提供的接口和应该满足的功能. 接口 实时竞价接口 实时竞价接口也是最核心的接口,对接的dsp需要向exchange提供竞价地址.每次有广告请求时,exchange ...