【leetcode】4. 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)).
解题思路:
这道题个人觉得是很难的,要想解出这个题,就必须了解一下几点:
1.求一列长度为len的数字的中位数,其实也可以被看做是求第len/2小的数(len为奇),或者求第len/2,len/2 +1小的数的平均值的数(len为偶);
2.同时还要注意若想求两个有序数组array1和array2中第n小的数,则可以取a,b满足a+b=n,则如果array1[a-1]<array2[b-1],则array1[a]必然小于第n小的数。可以将这些数排除后,继续求剩余的数中第n-a小的数就是所要的答案;
3.要想很快的缩小一个数的值到所求值,用折半的方法可以减少循环次数。
代码如下:
public class Solution {
public static double findMedianSortedArrays(int[] nums1, int[] nums2) {
int len1=nums1.length;
int len2=nums2.length;
if(((len1+len2)&1)==0){
int count=(len1+len2)/2;
int num1=fun(nums1,0,nums2,0,count);
int num2=fun(nums1,0,nums2,0,count+1);
return (double)(num1+num2)/2;
}
else{
int count=(len1+len2)/2 +1;
int num=fun(nums1,0,nums2,0,count);
return (double)num;
}
}
public static int fun(int[] nums1,int from1,int[] nums2,int from2,int k){
//考虑到某个数组中的数已经全部被排除
if(from1>=nums1.length){
return nums2[from2+k-1];
}
if(from2>=nums2.length){
return nums1[from1+k-1];
}
//k=1即求最小,比较两个数组中的from位置处的值即可
if(k==1){
return nums1[from1]<nums2[from2]?nums1[from1]:nums2[from2];
}
//为了减少代码量,要保证nums1的长度要小于nums2的长度
if(nums1.length-from1>nums2.length-from2){
int[] nums=nums1;
int from=from1;
nums1=nums2;
from1=from2;
nums2=nums;
from2=from;
}
//nums1最好能截取k/2位置处的数,但要保证不能超过nums1中还存在的数的个数
int len1 = Math.min(nums1.length-from1, k/2);
//保证len1+len2=k;
int len2 = k -len1;
if(nums1[from1+len1-1]>=nums2[from2+len2-1]){
from2=from2+len2;
int result = fun(nums1,from1,nums2,from2,k-len2);
return result;
}
else{
from1=from1+len1;
int result = fun(nums1,from1,nums2,from2,k-len1);
return result;
}
}
}
【leetcode】4. Median of Two Sorted Arrays的更多相关文章
- 【LeetCode】4. Median of Two Sorted Arrays (2 solutions)
Median of Two Sorted Arrays There are two sorted arrays A and B of size m and n respectively. Find t ...
- 【LeetCode】4. Median of Two Sorted Arrays 寻找两个正序数组的中位数
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:数组,中位数,题解,leetcode, 力扣,python ...
- 【LeetCode】004. Median of Two Sorted Arrays
题目: There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the ...
- 【LeetCode】4.Median of Two Sorted Arrays 两个有序数组中位数
题目: There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the ...
- 【LeetCode】4. Median of Two Sorted Arrays(思维)
[题意] 给两个有序数组,寻找两个数组组成后的中位数,要求时间复杂度为O(log(n+m)). [题解] 感觉这道题想法非常妙!! 假定原数组为a,b,数组长度为lena,lenb. 那么中位数一定是 ...
- 【一天一道LeetCode】#4 Median of Two Sorted Arrays
一天一道LeetCode (一)题目 There are two sorted arrays nums1 and nums2 of size m and n respectively. Find th ...
- 【leetcode】1213.Intersection of Three Sorted Arrays
题目如下: Given three integer arrays arr1, arr2 and arr3 sorted in strictly increasing order, return a s ...
- 【LeeetCode】4. 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 ...
- 【medium】4. Median of Two Sorted Arrays 两个有序数组中第k小的数
There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...
随机推荐
- HashMap解决hash冲突的方法
HashMap 采用一种所谓的“Hash 算法”来决定每个元素的存储位置.当程序执行 map.put(String,Obect)方法 时,系统将调用String的 hashCode() 方法得到其 h ...
- 用C#调用Matlab图像处理自制QQ游戏2D桌球瞄准器
平时不怎么玩游戏,有时消遣就玩玩QQ里的2D桌球,但是玩的次数少,不能像骨灰级玩家一样百发百中,肿么办呢?于是某天突发奇想,决定自己也来做个“外挂”.说是外挂,其实只是一个瞄准器,毕竟外挂是修改别人的 ...
- Jordan Lecture Note-12: Kernel典型相关分析(Kernel Canonical Correlation Analysis, KCCA).
Kernel典型相关分析 (一)KCCA 同样,我们可以引入Kernel函数,通过非线性的坐标变换达到之前CCA所寻求的目标.首先,假设映射$\Phi_X: x\rightarrow \Phi_X(x ...
- C++_快速排序(纯C版本)
//比较大小 static int compare_int(const void *int1,const void *int2) { if(*(const int*)int1>*(const i ...
- android Button 切换背景,实现动态按钮和按钮颜色渐变
android Button 切换背景,实现动态按钮和按钮颜色渐变 一.添加android 背景筛选器selector实现按钮背景改变 1.右键单击项目->new->Oth ...
- spring源码分析之spring-messaging模块详解
0 概述 spring-messaging模块为集成messaging api和消息协议提供支持. 其代码结构为: 其中base定义了消息Message(MessageHeader和body).消息处 ...
- uva 784 Maze Exploration 染色 搜索水题 DFS
染色问题,其实就是看看图上某一点能扩散多少. 用DFS解决,因为BFS不是很熟 =-=...以后要多练. 提交后32ms,优化了一下,在递归前进行判定,优化到22ms,不是优化的很好... 代码: # ...
- maven管理多模块系统
1.创建mydemo项目cd d:\myworkmvn archetype:create -DgroupId=com.example.mydemo -DartifactId=mydemo 生成myde ...
- use selenium in scrapy webdriver
https://pypi.python.org/pypi/selenium from selenium import webdriver from selenium.webdriver.common. ...
- 对于android触摸事件模型的一些理解
body{ font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI& ...