Median of Two Sorted Arrays 解答
Question
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)).
Solution 1 -- Traverse Array
Use merge procedure of merge sort here. Keep track of count while comparing elements of two arrays. Note to consider odd / even situation.
Time complexity O(n), space cost O(1).
 public class Solution {
     public double findMedianSortedArrays(int[] nums1, int[] nums2) {
         int length1 = nums1.length, length2 = nums2.length, total = length1 + length2;
         int index1 = total / 2, index2;
         // Consider two situations: (n + m) is odd, (n + m) is even
         if (total % 2 == 0)
             index2 = total / 2 - 1;
         else
             index2 = total / 2;
         // Traverse once to get median
         int p1 = 0, p2 = 0, p = -1, tmp, median1 = 0, median2 = 0;
         while (p1 < length1 && p2 < length2) {
             if (nums1[p1] < nums2[p2]) {
                 tmp = nums1[p1];
                 p1++;
             } else {
                 tmp = nums2[p2];
                 p2++;
             }
             p++;
             if (p == index1)
                 median1 = tmp;
             if (p == index2)
                 median2 = tmp;
         }
         if (p < index1 || p < index2) {
             while (p1 < length1) {
                 tmp = nums1[p1];
                 p1++;
                 p++;
                 if (p == index1)
                     median1 = tmp;
                 if (p == index2)
                     median2 = tmp;
             }
             while (p2 < length2) {
                 tmp = nums2[p2];
                 p2++;
                 p++;
                 if (p == index1)
                     median1 = tmp;
                 if (p == index2)
                     median2 = tmp;
             }
         }
         return ((double)median1 + (double)median2) / 2;
     }
 }
Solution 2 -- Binary Search
General way to find Kth element in two sorted arrays. Time complexity O(log(n + m)).
 public class Solution {
     public double findMedianSortedArrays(int[] nums1, int[] nums2) {
         int m = nums1.length, n = nums2.length;
         if ((m + n) %2 == 1)
             return findKthElement(nums1, nums2, (m + n) / 2, 0, m - 1, 0, n - 1);
         else
             return (findKthElement(nums1, nums2, (m + n) / 2, 0, m - 1, 0, n - 1) * 0.5 +
                     findKthElement(nums1, nums2, (m + n) / 2 - 1, 0, m - 1, 0, n - 1) * 0.5);
     }
     private double findKthElement(int[] A, int[] B, int k, int startA, int endA, int startB, int endB) {
         int l1 = endA - startA + 1;
         int l2 = endB - startB + 1;
         if (l1 == 0)
             return B[k + startB];
         if (l2 == 0)
             return A[k + startA];
         if (k == 0)
             return A[startA] > B[startB] ? B[startB] : A[startA];
         int midA = k * l1 / (l1 + l2);
         // Note here
         int midB = k - midA - 1;
         midA = midA + startA;
         midB = midB + startB;
         if (A[midA] < B[midB]) {
             k = k - (midA - startA + 1);
             endB = midB;
             startA = midA + 1;
             return findKthElement(A, B, k, startA, endA, startB, endB);
         } else if (A[midA] > B[midB]) {
             k = k - (midB - startB + 1);
             endA = midA;
             startB = midB + 1;
             return findKthElement(A, B, k, startA, endA, startB, endB);
         } else {
             return A[midA];
         }
     }
 }
Median of Two Sorted Arrays 解答的更多相关文章
- 【算法之美】求解两个有序数组的中位数 — leetcode 4. Median of Two Sorted Arrays
		
一道非常经典的题目,Median of Two Sorted Arrays.(PS:leetcode 我已经做了 190 道,欢迎围观全部题解 https://github.com/hanzichi/ ...
 - [LintCode] Median of Two Sorted Arrays 两个有序数组的中位数
		
There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted ...
 - 2.Median of Two Sorted Arrays (两个排序数组的中位数)
		
要求:Median of Two Sorted Arrays (求两个排序数组的中位数) 分析:1. 两个数组含有的数字总数为偶数或奇数两种情况.2. 有数组可能为空. 解决方法: 1.排序法 时间复 ...
 - 【转载】两个排序数组的中位数 / 第K大元素(Median of Two Sorted Arrays)
		
转自 http://blog.csdn.net/zxzxy1988/article/details/8587244 给定两个已经排序好的数组(可能为空),找到两者所有元素中第k大的元素.另外一种更加具 ...
 - LeetCode 4 Median of Two Sorted Arrays (两个数组的mid值)
		
题目来源:https://leetcode.com/problems/median-of-two-sorted-arrays/ There are two sorted arrays nums1 an ...
 - No.004 Median of Two Sorted Arrays
		
4. Median of Two Sorted Arrays Total Accepted: 104147 Total Submissions: 539044 Difficulty: Hard The ...
 - 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(3) || Median of Two Sorted Arrays
		
LeetCode(3) || Median of Two Sorted Arrays 题记 之前做了3题,感觉难度一般,没想到突然来了这道比较难的,星期六花了一天的时间才做完,可见以前基础太差了. 题 ...
 - Kotlin实现LeetCode算法题之Median of Two Sorted Arrays
		
题目Median of Two Sorted Arrays(难度Hard) 方案1,数组合并&排序调用Java方法 import java.util.* class Solution { fu ...
 
随机推荐
- 福建省队集训被虐记——DAY2
			
唉--第二天依然被虐--但是比第一天好一点--我必须负责任的指出:志灿大神出的题比柯黑的不知道靠谱到哪里去了--柯黑出的简直不可做 但是被虐的命运是无法改变的--求各位神犇别D我 黄巨大真是强啊,不愧 ...
 - HDU  1251  字典树(前缀树)
			
题目大意 :Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己的前缀).(单词互不相同) ...
 - Uva227.Puzzle
			
题目连接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
 - [置顶] Objective-C ,ios,iphone开发基础:自定义控件:Eg: UIButton
			
第一步:新建一个工程,在 .h文件中坐如下声明: #import <UIKit/UIKit.h> @interface MyButtonViewController : UIViewCon ...
 - JUnit单元测试框架的使用
			
http://blog.csdn.net/mao520741111/article/details/51462215 原文地址 http://www.open-open.com/lib/view/op ...
 - spring MVC上传文件演示
			
//相比smartUpload功能上感觉确实有点心有意力不足的感觉,就安全性判断后缀,smartUpload就非常方便. public ModelAndView addFileUp(HttpServl ...
 - pyqt 图片(label上显示
			
# -*- coding: utf-8 -*- # python:2.x __author__ = 'Administrator' from decimal import * from PyQt4.Q ...
 - 关于scrollTop
			
如下图
 - Lambda表达式的面纱(一)
			
在.NET3.0版本中微软推出了Lambda表达式.这使代码的表述可以更加优雅.但是对于新事物大多会本能的排斥,虽然3.0版本已经推出了好久了,但是我向周围的人了解了一下,用Lambda的人不是很多, ...
 - Centos6架设GIT服务,windows客户端使用TortoiseGit加载KEYGEN连接GIT服务器
			
前几天得空,想起前一阵学了GIT还没好好实践,就在虚拟机中安装测试了一下,并简单记录了CENTOS6中GIT安装,ssh-keygen生成,客户端使用TortoiseGit加载KEYGEN连接GIT服 ...