题目要求:

有两个排序的数组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. MySQL---下载安装、数据库基本操作

    1.下载安装 1.1 下载:  http://dev.mysql.com/downloads/mysql/ 1.2 解压 1.3 初始化 cd c:\mysql-5.7.16-winx64\bin ( ...

  2. Linux入门——SSH免密登录

    SSH免密登录 1.简介 SSH是一种网络协议,用于计算机之间的加密登录. 本文针对的实现是OpenSSH,它是自由软件,应用非常广泛. 2.初始化公钥私钥 有rsa,dsa两种加密方式,生成的公钥私 ...

  3. C语言#ifdef等宏的妙用

    这几个宏是为了进行条件编译.一般情况下,源程序中所有的行都参加编译.但是有时希望对其中一部分内容只在满足一定条件才进行编译,也就是对一部分内容指定编译的条件,这就是“条件编译”.有时,希望当满足某条件 ...

  4. 第二节 双向链表的GO语言实现

    一.什么是双向链表 和单链表比较,双向链表的元素不但知道自己的下线,还知道自己的上线(越来越像传销组织了).小煤车开起来,图里面可以看出,每个车厢除了一个指向后面车厢的箭头外,还有一个指向前面车厢的箭 ...

  5. 【Hutool】Hutool工具类之Http工具——HttpUtil

    最简单最直接的上手可以参见参考文档:http://hutool.mydoc.io/?t=216015   Http协议的介绍,请参考web随笔:http://www.cnblogs.com/jiang ...

  6. 【转】odoo学习之:Environment

    Environment类提供了对ORM对象的封装,同时提供了对注册类的访问,记录集的缓存,以及管理重计算的数据结构. 对于继承了Model的类来说可以直接通过self.env对Environment进 ...

  7. 关于 logger

    日志 前言 我是一名后台程序员,接触后台只有一年时间,在这期间一共做过四个项目,分别是: 工作室招新系统 视频学习网站 创客网站 打印机项目 由于之前做项目的时候没有好好重视日志,所以导致在开发与维护 ...

  8. Codeforces Contest 870 前三题KEY

    A. Search for Pretty Integers: 题目传送门 题目大意:给定N和M个数,从前一个数列和后一个数列中各取一个数,求最小值,相同算一位数. 一道水题,读入A.B数组后枚举i.j ...

  9. 成都Uber优步司机奖励政策(2月2日)

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

  10. Redis系列八 使用Jedis

    使用Jedis jar操作Redis 1.配置redis.conf文件,修改 2.建java工程,加入 jedis jar包 3.代码示例: package com.ntjr.redis; impor ...