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 ...
随机推荐
- Spring MVC基础
1.Web MVC基础 MVC的本质是表现层模式,我们以视图模型为中心,将视图和控制器分离出来.就如同分层模式一样,我们以业务逻辑为中心,把表现层和数据访问层代码分离出来是一样的方法.框架只能在技术层 ...
- linux之chdir函数解析
[lingyun@localhost chdir]$ ls chdir.c [lingyun@localhost chdir]$ cat chdir.c /********************* ...
- Qwerty78 Trip(组合数,规律,逆元)
Qwerty78 Trip time limit per test 2 seconds memory limit per test 64 megabytes input standard input ...
- iOS ARC注释和错误的解决方法在使用
1.一个错误The current deployment target does not support automated __weak references 这个错误被所述支持iOS版本号不支持相 ...
- Objective-C(十八、谓语使用及实例说明)——iOS开发基础
结合之前的学习笔记以及參考<Objective-C编程全解(第三版)>,对Objective-C知识点进行梳理总结.知识点一直在变,仅仅是作为參考,以苹果官方文档为准~ 十八.谓语的使用及 ...
- [HeadFrist-HTMLCSS学习笔记][认识HTML中的“HT”]
学习超链接 超链接 使用\元素创建一个超文本链接,链接到另一个Web 页面. \元素的内容会变成为Web页面中可单击的文本.href属性告诉浏览器链接的目标文件 <a href="el ...
- 使用react-native做一个简单的应用-06商品界面的实现
商品界面实现起来很简单,其实就是一个listview的使用: 关于listview的使用,在官方文档里面已经介绍的很详细了.在这里我要提一个坑. listview在Android和iOS中的效果是不一 ...
- web验证码
前台引用.aspx: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="YanZh ...
- hdu1175连连看
Problem Description “连连看”相信很多人都玩过.没玩过也没关系,下面我给大家介绍一下游戏规则:在一个棋盘中,放了很多的棋子.如果某两个相同的棋子,可以通过一条线连起来(这条线不能经 ...
- C++程序设计实践指导1.10二维数组元素换位改写要求实现
改写要求1:改写为以单链表和双向链表存储二维数组 改写要求2:添加函数SingleLinkProcess()实现互换单链表中最大结点和头结点位置,最小结点和尾结点位置 改写要求3:添加函数Double ...