Median of Two Sorted Arrays
来源:互联网 发布:图片文字制作软件 编辑:程序博客网 时间:2024/06/03 20:12
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)).
我的时间复杂度为O(n),其实仅为(m+n)/2
public class Solution {
public double findMedianSortedArrays(int[] nums1, int[] nums2) {int len1 = nums1.length, len2 = nums2.length;
if(len1 == 0 && len2 == 0){
return 0;
}
int cou = len1 + len2;
int median = 0;
boolean isEven = false;
if(cou % 2 == 1){
median = cou/2+1;
}else{
median = cou/2;
isEven = true;
}
int i = 0, j = 0;
while(i+j < median-1){
if(i < len1 && j < len2){
if(nums1[i] <= nums2[j]){
i++;
}else{
j++;
}
}else if(j < len2){
j++;
}else{
i++;
}
}
if(isEven){
if(i < len1 && j < len2){
int i1 = 0, i2 = 0;
if(nums1[i] < nums2[j]){
i1 = nums1[i];
if(i+1<len1){
i2 = Math.min(nums1[i+1], nums2[j]);
}else{
i2 = nums2[j];
}
}else if(nums1[i] > nums2[j]){
i1 = nums2[j];
if(j+1 < len2){
i2 = Math.min(nums1[i], nums2[j+1]);
}else{
i2 = nums1[i];
}
}else{
i1 = nums1[i];
i2 = nums2[j];
}
return (i2+i1)/2.0;
}else if(i < len1){
return (nums1[i]+nums1[i+1])/2.0;
}else{
return (nums2[j]+nums2[j+1])/2.0;
}
}else{
if(i < len1 && j < len2){
return Math.min(nums1[i], nums2[j]);
}else if(i < len1){
return nums1[i];
}else{
return nums2[j];
}
}
}
}
0 0
- Median of Two Sorted Arrays
- Median of Two Sorted Arrays
- Median of Two Sorted Arrays
- Median of two sorted arrays
- Median of Two Sorted Arrays
- Median of Two Sorted Arrays
- Median of Two Sorted Arrays
- Median of Two Sorted Arrays
- Median of Two Sorted Arrays
- Median of Two Sorted Arrays
- median-of-two-sorted-arrays
- Median of Two Sorted Arrays
- Median of Two Sorted Arrays
- Median of Two Sorted Arrays
- Median of Two Sorted Arrays
- Median of Two Sorted Arrays
- Median of Two Sorted Arrays
- Median of Two Sorted Arrays
- SSH中初次使用Ajax
- centOs 开发环境,jdk,mysql,tomcat
- org.tinygroup.context-上下文环境
- ios项目删除Git
- Werther Quest 笔记
- Median of Two Sorted Arrays
- IOS8开发技术之自动布局
- C# 简单工厂模式
- jquery动态生成表单元素
- org.tinygroup.commons-常用工具类
- 杂记
- PLSQL:[1]plsql中文乱码,显示问号
- vim操作指南,主要是文本跳转选择之类
- org.tinygroup.context2object-参数对象构建