博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
leetcode--Median of Two Sorted Arrays
阅读量:5214 次
发布时间:2019-06-14

本文共 2436 字,大约阅读时间需要 8 分钟。

There are two sorted arrays A and B 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)).

 

method 1, time complexity = O(max{m,n})

 

public class Solution {    public double findMedianSortedArrays(int A[], int B[]) {        double result = 0;		if(A.length != 0 || B.length != 0){			if(A.length == 0)				result = (B[(B.length - 1)/2] + B[B.length/2]) / 2.0;			if(B.length == 0)				result = (A[(A.length - 1)/2] + A[A.length/2]) / 2.0;			int length = A.length + B.length;			int[] total = new int[length];						int index1 = 0;			int index2 = 0;			for(int i = 0; i < length / 2 + 1; ++ i){				if(index1 < A.length && index2 < B.length){					if(A[index1] <= B[index2]){						total[i] = A[index1];						++index1;					}					else{						total[i] = B[index2];						++index2;					}				}				else if(index1 == A.length){					total[i] = B[index2];					++index2;				}				else{					total[i] = A[index1];					++index1;				}			}			result = (total[(length - 1)/2] + total[length /2]) / 2.0;		}		return result;    }}

  

 

The O(log(m + n)) method

public class Solution {    public double findMedianSortedArrays(int A[], int B[]) {       int alen = A.length, blen = B.length;       if((alen + blen) % 2 != 0) //odd number of elements in total    	   return findHelper(A, B, (alen + blen) / 2 + 1, 0, alen - 1, 0, blen - 1);       else //even number of elements in total    	   return (findHelper(A, B, (alen + blen) / 2, 0, alen - 1, 0, blen - 1)     			 + findHelper(A, B, (alen + blen) / 2 + 1, 0, alen - 1, 0, blen - 1)) / 2.0;    }	private int findHelper(int A[], int B[], int k, int startA, int endA, int startB, int endB){		if(endA < startA)			return B[startB + k - 1];		if(endB < startB)			return A[startA + k - 1];		if(k == 1)			return Math.min(A[startA], B[startB]);				int lenA = endA - startA + 1;		int lenB = endB - startB + 1;				if(lenA > lenB)			return findHelper(B, A, k, startB, endB, startA, endA);		else {			int aMid = Math.min(k / 2, lenA);			int bMid = k - aMid;			if(A[startA + aMid - 1] < B[startB + bMid - 1])				return findHelper(A, B, k - aMid, startA + aMid, endA, startB, endB);			else if(A[startA + aMid - 1] > B[startB + bMid - 1])				return findHelper(A, B, k - bMid, startA, endA, startB + bMid, endB);			else //A[startA + aMid - 1] == B[startB + bMid - 1]				return A[startA + aMid - 1];		}    }}

  

转载于:https://www.cnblogs.com/averillzheng/p/3540115.html

你可能感兴趣的文章
C# winform DataGridView 常见属性
查看>>
逻辑运算和while循环.
查看>>
Nhiberate (一)
查看>>
c#后台计算2个日期之间的天数差
查看>>
安卓开发中遇到的小问题
查看>>
ARTS打卡第3周
查看>>
HDU 2189 悼念512汶川大地震遇难同胞――来生一起走 --生成函数
查看>>
js知识梳理3:创建对象的模式探究
查看>>
linux后台运行和关闭SSH运行,查看后台任务
查看>>
cookies相关概念
查看>>
android动态权限获取
查看>>
CAN总线波形中ACK位电平为什么会偏高?
查看>>
siebel 中 join 使用心得
查看>>
剑指Offer:重建二叉树
查看>>
MyBatis课程2
查看>>
css属性之统一设置文本及div之间的对齐方式
查看>>
PHP大批量更新数据,大批量插入数据,mysql批量更新与插入多种方法
查看>>
[转]如何循序渐进向dotnet架构师发展
查看>>
桥接模式-Bridge(Java实现)
查看>>
dpi 、 dip 、分辨率、屏幕尺寸、px、density 关系以及换算(终结版)
查看>>