【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 ...
随机推荐
- C++ 预编译头文件
1.解决什么问题? C++ 编译器是单独,分别编译的,每个cpp文件,进行预编译(也就是对#include,define 等进行文本替换),生成编译单元.编译单元是一个自包含文件,C++编译器对编译单 ...
- BZOJ 1013: [JSOI2008]球形空间产生器sphere 高斯消元
1013: [JSOI2008]球形空间产生器sphere Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/Judg ...
- QT5 串口收发实例代码
以下代码是自己测试门禁系统使用的 主要用到了串口的接收和发送 开发环境:xp QT5.1.1 串口:38400 N 8 1 自动检测可用串口 在xp上测试没问题 有些usb转串口会出现波特 ...
- qsort函数、sort函数 (精心整理篇)
先说明一下qsort和sort,只能对连续内存的数据进行排序,像链表这样的结构是无法排序的. 首先说一下, qsort qsort(基本快速排序的方法,每次把数组分成两部分和中间的一个划分值,而对于有 ...
- Apache Shiro Architecture--官方文档
原文地址:http://shiro.apache.org/architecture.html Apache Shiro's design goals are to simplify applicati ...
- GLOG使用注意事项
FLAGS_stderrthreshold 输出到stderr的门限,默认为2(ERROR),默认,ERORR以下的信息不打印到终端 FLAGS_alsologtostderr 当这个全局变量 ...
- Cannot start service SPUserCodeV4 on computer
Cannot start service SPUserCodeV4 on computer SharePoint2010 部署问题: 当Visual Studio 2010中尝试部署Sharepoin ...
- poj1068解题报告(模拟类)
POJ 1068,题目链接http://poj.org/problem?id=1068 题意: 对于给出给出的原括号串S,对应两种数字密码串P.W: S (((()()()))) P- ...
- XML XmlDocument
1.SelectSingleNode(xPath) xPxth:xml/cam/type 一层一层递进
- pgpool postgresql集群、中间件
pgpool-II是一个工作于PostgreSQL服务器端和PostgreSQL客户端之间的中间件,它提供了如下的功能: 1.连接池 pgpool-II中保存了到PostgreSQL服务器的连接,然后 ...