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 ...
随机推荐
- canvas之背景特效
需具备js基础知识以及canvas相关方法(可查阅相关文档) 下面是一篇有关js与canvas的背景特效 基于面向过程的思维 <!DOCTYPE html> <html> &l ...
- ajaxSubmit 在ie9或360兼容中,form下是空的
解决办法:在<head>....</head>中加入<meta http-equiv="X-UA-Compatible" content=" ...
- thinkphp中的大字母的意思
ThinkPHP 单字母函数 A() 内部实例化控制器 D() 实例化自定义模型类 M() 实例化一个基础模型类 R() 调用某个控制器的操作方法 L() 启用多语言的情况下,设置和获取当前的语言定义 ...
- angular2路由之routerLinkActive指令
angular2的routerLinkActive指令在路由激活时添加样式class .red{ color: red;} <a routerLink="/user/login ...
- laravel 闪存
https://blog.csdn.net/ckdecsdn/article/details/52083093
- Dubbo client 启动报错:No provider available for the service use dubbo version 2.5.3
1.异常 java.lang.IllegalStateException: Failed to check the status of the service org.ko.server.servic ...
- Python 爬虫 (四)
requests: 练手 雪qiu网 import requests import json import re import pymysql url = 'https://xueqiu.com/v4 ...
- verilog中参数传递与参数定义中#的作用(二)
一.module内部有效的定义 用parameter来定义一个标志符代表一个常量,称作符号常量,他可以提高程序的可读性和可维护性.parameter是参数型数据的关键字,在每一个赋值语句的右边都必须是 ...
- PS灰度蒙版建立
通道里: 建立高光 1.Ctrl + 鼠标单击RGB通道-->得到高光 点击蒙版-->得到Alpha1(重命名高光通道): 2.然后紧接着Ctrl + shift + alt +鼠标单击A ...
- 北京Uber优步司机奖励政策(1月15日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...