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 ...
随机推荐
- Hdu4005-The war(双连通缩点)
In the war, the intelligence about the enemy is very important. Now, our troop has mastered the situ ...
- 系统中断与SA_RESTART
今天在调试程序时,sem_timedwait居然返回了一个Interrupted system call,错误码为EINTR.系统中断这东西我一向只闻其名,不见其"人",不想今天遇 ...
- IOS 监听通讯录是否改变
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &error); ABAddressBookRegist ...
- [置顶] Application,Session,Cookie之Application对象
概述 Application为全局作用域,且只有一个Application对象,它可以存储和访问任意页面的变量(数据存储类型都是Object,也就是任意类型),同时也被多页面使用(也为引用). App ...
- 本博客弃用,请移步http://ningios.com查看最新
本博客弃用,请移步http://ningios.com查看最新
- ASP.NET的Application简介1
ASP.NET中的Application 1. Application是用于保存所有用户共有的信息.在ASP时代,如果要保存的数据在应用程序生存期内不会或者很少改变,那么使用Application是理 ...
- Javascript页面跳转与浏览器兼容
用<meta>标签实现的定时跳转: <meta http-equiv="refresh" content="5 url=http://www.baidu ...
- node.js如何使用回调
Node.js到处使用回调,尤其在有I/O(输入/输出)操作的地方. 下面是在一个Node.js中使用filesystem模块中从磁盘上读入文件内容示例一: var fs = require('fs' ...
- poj2243 bfs
O - 上一个题的加强版 Crawling in process... Crawling failed Time Limit:1000MS Memory Limit:65536KB 6 ...
- CDZSC_2015寒假新人(2)——数学 G
G - G Time Limit:3000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Status ...