Median of Two Sorted Arrays(hard)
题目要求:
有两个排序的数组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)的更多相关文章
- 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 ...
- Leetcode 4. Median of Two Sorted Arrays(二分)
4. Median of Two Sorted Arrays 题目链接:https://leetcode.com/problems/median-of-two-sorted-arrays/ Descr ...
- 【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 ...
- 【LeetCode】4. Median of Two Sorted Arrays(思维)
[题意] 给两个有序数组,寻找两个数组组成后的中位数,要求时间复杂度为O(log(n+m)). [题解] 感觉这道题想法非常妙!! 假定原数组为a,b,数组长度为lena,lenb. 那么中位数一定是 ...
- leetcode 之Median of Two Sorted Arrays(五)
找两个排好序的数组的中间值,实际上可以扩展为寻找第k大的数组值. 参考下面的思路,非常的清晰: 代码: double findMedianofTwoSortArrays(int A[], int B[ ...
- 【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 ...
- 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 ...
- [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 ...
- 60.Median of Two Sorted Arrays(两个排序数组的中位数)
Level: Hard 题目描述: There are two sorted arrays nums1 and nums2 of size m and n respectively. Find t ...
随机推荐
- laravel 安装添加多站点
官方文档如下 https://learnku.com/laravel/t/1160/laravel-nginx-multi-site-configuration
- python学习——面向对象的三大特性
一.继承 继承是一种创建新类的方式,在python中,新建的类可以继承一个或多个父类,父类又可称为基类或超类,新建的类称为派生类或子类. 1.python中类的继承分为:单继承和多继承 class P ...
- Python 爬虫 招聘信息并存入数据库
新学习了selenium,啪一下腾讯招聘 from lxml import etree from selenium import webdriver import pymysql def Geturl ...
- QOS-policy配置
QOS-QOS-policy配置 2018年7月7日 20:29 配置: 先匹配acl: [RT2]acl number 3000 [RT2-acl-adv-3000]description QOS ...
- linux redhat NFS网络共享搭建
nfs网络共享 测试环境: 服务端:redhat6.7 ip:192.168.1.100 客户端:redhat6.7 ip:192.168.1.110 一.服务端 1.创建共享文件夹 权限666即可 ...
- 怎样才能使用ChipScope 加入被优化掉的信号
在调试过程中常常遇到的一个问题就是,xilinx工具在逻辑综合的过程中,将自己RTL代码中的很多变量都优化掉了,使得调试的抓信号的过程很纠结.以下是解决方法: 1.右键synthesis,在综合选项里 ...
- Understanding Delegated JavaScript Events
While I ended up using a CSS-only implementation for this pen, I started by writing it mostly using ...
- Caliburn.Micro 杰的入门教程3,事件和参数
Caliburn.Micro 杰的入门教程1(翻译)Caliburn.Micro 杰的入门教程2 ,了解Data Binding 和 Events(翻译)Caliburn.Micro 杰的入门教程3, ...
- DevExpress 操作gridcontrol
XtraGrid的关键类就是:GridControl和GridView.GridControl本身不显示数据,数据都是显示在GridView/CardView/XXXXView中.GridContro ...
- MySQL高级-锁机制
一.概述 1.定义 2.锁的分类 ①从对数据操作的类型(读\写)分 读锁(共享锁):针对同一份数据,多个读操作可以同时进行而不会互相影响. 写锁(排它锁):当前写操作没有完成前,它会阻断其他写锁和读锁 ...