题目要求:

有两个排序的数组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. 小心使用replicate_do_db和replicate_ignore_db

    内容来源于网络 使用replicate_do_db和replicate_ignore_db时有一个隐患,跨库更新时会出错 如设置 replicate_do_db=testuse mysql;updat ...

  2. laravel4.2 Redis 使用

    laravel4.2 Redis 使用 配置文件,app/config/database.php 'redis' => array( 'cluster' => false, 'defaul ...

  3. For-each Loop,Index++ Loop , Iterator 那个效率更高

    平时在写Java/C# 程序的时候,会写很多的Loop 语句,for() 及 Iterator loop 及Java 8 的foreach Loop, 这些Loop 那种效率最高呢?写个小程序测试一下 ...

  4. rails应用无法读取kafka数据报错Kafka::Error: Failed to find group coordinator

    如果确保kafka中有数据,rails应用中却无法读取到,或报如下错误: Kafka::Error: Failed to find group coordinator   一般有两种情况,解决:   ...

  5. arping命令用法

    arping命令使用说明 BusyBox v1.17.3 (2011-07-20 17:01:30 CST) multi-call binary. Usage: arping [-fqbDUA] [- ...

  6. 自己用原生JS写的轮播图,支持移动端触屏滑动,面向对象思路。分页器圆点支持click和mouseover。

    自己用原生javascript写的轮播图,面向对象思路,支持移动端手指触屏滑动.分页器圆点可以选择click点击或mouseover鼠标移入时触发.图片滚动用的setInterval,感觉setInt ...

  7. Splay初学习

    例题传送门 听YZ哥哥说Splay是一种很神奇的数据结构,所以学习了一下它的最基本操作.O(1)的Spaly. 伸展树(Splay Tree),也叫分裂树,是一种二叉排序树,它能在O(logn)内完成 ...

  8. 北京Uber优步司机奖励政策(1月14日)

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

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

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

  10. CF 314 E. Sereja and Squares

    E. Sereja and Squares http://codeforces.com/contest/314/problem/E 题意: 给你一个擦去了部分左括号和全部右括号的括号序列,括号有25种 ...