题目要求:

有两个排序的数组nums1和nums2分别为m和n大小。

找到两个排序数组的中位数。整体运行时间复杂度应为O(log(m + n))。

示例:

我的方法:

  分别逐个读取两个数组的数,放到一个新的数组里,由于两个数组本身是已经排序好的,所以只需要在放在新数组时候注意对比,放入完成后,就是一个排序好的数组了,就不需要重新排序增加时间复杂度。然后再找出中位数。

public static double getMediumNum(int[] nums1, int[] nums2){
int m = nums1.length;
int n = nums2.length;
int l = n + m;
int[] nums = new int[l];
int i=0, j=0, t=0;
for(; t<l && (i<m) && (j<n); t++){
if(nums1[i] < nums2[j]){
nums[t] = nums1[i];
i++;
}else{
nums[t] = nums2[j];
j++;
}
}
if(i == m && t != l){
for(;t<l;t++,j++){
nums[t] = nums2[j];
}
}else if(j == n && t != l){
for(;t<l;t++,i++){
nums[t] = nums1[i];
}
}
for(int a=0; a<nums.length; a++){
System.out.println(nums[a]);
}
if(l%2 == 0){
return ((double)nums[l/2] + (double)nums[l/2-1])/2;
}
return (double)nums[l/2];
}

leetcode方法:

 public double findMedianSortedArrays(int[] A, int[] B) {
int m = A.length;
int n = B.length;
if (m > n) { // to ensure m<=n
int[] temp = A; A = B; B = temp;
int tmp = m; m = n; n = tmp;
}
int iMin = 0, iMax = m, halfLen = (m + n + 1) / 2;
while (iMin <= iMax) {
int i = (iMin + iMax) / 2;
int j = halfLen - i;
if (i < iMax && B[j-1] > A[i]){
iMin = iMin + 1; // i is too small
}
else if (i > iMin && A[i-1] > B[j]) {
iMax = iMax - 1; // i is too big
}
else { // i is perfect
int maxLeft = 0;
if (i == 0) { maxLeft = B[j-1]; }
else if (j == 0) { maxLeft = A[i-1]; }
else { maxLeft = Math.max(A[i-1], B[j-1]); }
if ( (m + n) % 2 == 1 ) { return maxLeft; } int minRight = 0;
if (i == m) { minRight = B[j]; }
else if (j == n) { minRight = A[i]; }
else { minRight = Math.min(B[j], A[i]); } return (maxLeft + minRight) / 2.0;
}
}
return 0.0;
}

Median of Two Sorted Arrays(hard)的更多相关文章

  1. 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 ...

  2. Leetcode 4. Median of Two Sorted Arrays(二分)

    4. Median of Two Sorted Arrays 题目链接:https://leetcode.com/problems/median-of-two-sorted-arrays/ Descr ...

  3. 【leetcode】Median of Two Sorted Arrays(hard)★!!

    There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted ...

  4. 【LeetCode】4. Median of Two Sorted Arrays(思维)

    [题意] 给两个有序数组,寻找两个数组组成后的中位数,要求时间复杂度为O(log(n+m)). [题解] 感觉这道题想法非常妙!! 假定原数组为a,b,数组长度为lena,lenb. 那么中位数一定是 ...

  5. leetcode 之Median of Two Sorted Arrays(五)

    找两个排好序的数组的中间值,实际上可以扩展为寻找第k大的数组值. 参考下面的思路,非常的清晰: 代码: double findMedianofTwoSortArrays(int A[], int B[ ...

  6. 【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 tw ...

  7. LeetCode第[4]题(Java):Median of Two Sorted Arrays (俩已排序数组求中位数)——HARD

    题目难度:hard There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median ...

  8. [LeetCode] 4. Median of Two Sorted Arrays(想法题/求第k小的数)

    传送门 Description There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the m ...

  9. 60.Median of Two Sorted Arrays(两个排序数组的中位数)

    Level:   Hard 题目描述: There are two sorted arrays nums1 and nums2 of size m and n respectively. Find t ...

随机推荐

  1. animation(动画)设置

    1.animation 动画 概念:当您在 @keyframes 中创建动画时,请把它捆绑到某个选择器,否则不会产生动画效果. 通过规定至少以下两项 CSS3 动画属性,即可将动画绑定到选择器: 规定 ...

  2. OrientDB部署

    1. 环境准备 操作系统: Centos6.8 内存: 8G(分布式部署时建议4G及以上,否则需要手动修改JVM配置) JDK: 建议jdk8版本(3.0版本要求jdk8) 环境变量:需配置JAVA_ ...

  3. TypeScript : 语法及特性

    当let声明一个变量的时候它使用的词法作用域或者是块作用域.块作用域指的就是他们包含的块以外的不能访问. const声明:是let声明有相同的作用域规则,但是它被赋值后不能再被改变.类似于java的f ...

  4. IO流之字符流

    字符流产生的原因: 1.每次只能够读取一个字节或者一个字节数组,每次在需要转换成字符或者字符串的时候不是很方便2.不同的操作系统针对换行符的处理不方便3.有的时候会出现中文乱码(中文占两个字节,如果针 ...

  5. 行人属性识别 PETA数据集

    参见https://blog.csdn.net/hyk_1996/article/details/80322026(内有数据集下载地址) 其中footwearSneaker属性应该是footwearS ...

  6. 洛谷P2252 取石子游戏(威佐夫博弈)

    题目背景 无 题目描述 有两堆石子,数量任意,可以不同.游戏开始由两个人轮流取石子.游戏规定,每次有两种不同的取法,一是可以在任意的一堆中取走任意多的石子:二是可以在两堆中同时取走相同数量的石子.最后 ...

  7. 武汉Uber优步司机奖励政策(12月21日-12.27日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  8. docker 在window 10 专业版的安装 && .net core 在docker的部署

    1.如果无法安装Hyper-V,八成是自己的杀毒软件给关了,我的是 电脑管家-启动项里面 给关掉了. 2.如果部署.net core 后 运行 报 An assembly specified in t ...

  9. CC2541广播机制和代码分析(未完成)

    1. 广播通道有3个,是固定的吗?设备为了节省功耗,可以忽略掉几个应答? 连接间隔可以是7.5ms到4s内的任意值,但必须是1.25ms的整数倍,从设备延迟,实际上是一个连接间隔的倍数,代表从设备在必 ...

  10. ajax跨域请求php

    在众多站群中,不同功能的系统使用独立的一个域名,各系统之间存在相互调用的关系.使用js的XMLHttpRequest调用其他域名提示跨域权限不足.有些可能认为都同属于同一个顶级域名或者说域名一模一样怎 ...