LeetCode——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)).
Example 1:
nums1 = [1, 3]
nums2 = [2]
The median is 2.0
Example 2:
nums1 = [1, 2]
nums2 = [3, 4]
The median is (2 + 3)/2 = 2.5
Solution
二分查找。 时间复杂度O(lgn)
- 将第一个数组划分成两部分
left_A | right_A
A[0], A[1], ..., A[i-1] | A[i], A[i+1], ..., A[m-1]
- 将第二个数组划分成两部分
left_B | right_B
B[0], B[1], ..., B[j-1] | B[j], B[j+1], ..., B[n-1]
- 两个左半部分小于两个右半部分
left_part | right_part
A[0], A[1], ..., A[i-1] | A[i], A[i+1], ..., A[m-1]
B[0], B[1], ..., B[j-1] | B[j], B[j+1], ..., B[n-1]
- 条件
1) len(left_part) == len(right_part)
2) max(left_part) <= min(right_part)
我们可以看到因为左右的长度一样,都等于两个数组长度之和的一半。所以i的位置确定,那么j的位置也确定了,所以我们只需要找一个合适的i,使得满足以上两个条件就可以了。
Code
class Solution {
public:
double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
int m = nums1.size();
int n = nums2.size();
if (m > n) {
vector<int> tmp = nums1;
nums1 = nums2;
nums2 = tmp;
m = nums1.size();
n = nums2.size();
}
if (m == 0) {
if (n % 2 == 1) {
return nums2[n / 2];
} else {
return ( nums2[n / 2 - 1] + nums2[n / 2] ) / 2.0;
}
}
int imin = 0;
int imax = m;
// 因为可能是奇数个,所以得加1
int half_len = (m + n + 1) / 2;
while (imin <= imax) {
int i = (imin + imax) >> 1;
int j = half_len - i;
// 说明i太大了,需要减小
if (i > 0 && nums1[i - 1] > nums2[j]) {
imax = i - 1;
}
// 说明i太小了,需要增大
else if (i < m && nums2[j - 1] > nums1[i]) {
imin = i + 1;
}
else {
int left_max;
int right_min;
// i等于0,说明nums1,全属于右边
if (i == 0) left_max = nums2[j - 1];
// j等于0,说明nums2,全属于右边
else if (j == 0) left_max = nums1[i - 1];
else left_max = max(nums1[i - 1], nums2[j - 1]);
if ((m + n) % 2 == 1)
return left_max;
// i等于m说明nums1,全属于左边
if (i == m) right_min = nums2[j];
// j等于n说明nums2,全属于左边
else if (j == n) right_min = nums1[i];
else right_min = min(nums1[i], nums2[j]);
return (left_max + right_min) / 2.0;
}
}
}
};
LeetCode——Median of Two Sorted Arrays的更多相关文章
- LeetCode: Median of Two Sorted Arrays 解题报告
Median of Two Sorted Arrays There are two sorted arrays A and B of size m and n respectively. Find t ...
- [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 two ...
- [leetcode]Median of Two Sorted Arrays @ Python
原题地址:https://oj.leetcode.com/problems/median-of-two-sorted-arrays/ 题意:There are two sorted arrays A ...
- Leetcode 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 ...
- LeetCode—— Median of Two Sorted Arrays
Description: There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the medi ...
- Leetcode: Median of Two Sorted Arrays. java.
There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted ...
- C++ Leetcode Median of Two Sorted Arrays
坚持每天刷一道题的小可爱还没有疯,依旧很可爱! 题目:There are two sorted arrays nums1 and nums2 of size m and n respectively. ...
- leetcode:Median of Two Sorted Arrays分析和实现
这个问题的大意是提供两个有序的整数数组A与B,A与B并集的中间数.[1,3]与[2]的中间数为2,因为2能将A与B交集均分.而[1,3]与[2,4]的中间数为2.5,取2与3的平均值.故偶数数目的中间 ...
- LeetCode Median of Two Sorted Arrays 找中位数(技巧)
题意: 给两个有序(升or降)的数组,求两个数组合并之后的中位数. 思路: 按照找第k大的思想,很巧妙.将问题的规模降低,对于每个子问题,k的规模至少减半. 考虑其中一个子问题,在两个有序数组中找第k ...
随机推荐
- ubuntu android 设备识别 Setting up a Device for Development
参考: http://developer.android.com/tools/device.html lsusb Bus 001 Device 004: ID 18d1:9025 Google I ...
- Web项目管理工具精选(上)
原文:Web项目管理工具精选(上) 随着新兴科技公司的蓬勃发展,不少Web应用和浏览器工具在开发者.设计者.自由职业者和项目经理中间流行开来.这些工具在不断发展,我们也看到越来越多的桌面应用.移动应用 ...
- js如何转义和反转义html特殊字符
“<”如何反转义为“<”,“>”如何反转义为“>”,下面就介绍如何用js来实现这种类似的操作. //HTML转义 function HTMLEncode(html) { var ...
- 六、Mosquitto 高级应用之SSL/TLS
mosquitto提供SSL支持加密的网络连接和身份验证.本章节讲述次功能的实现. 在此之前需要一些准备工作. 准本工作: 一台 Linux 服务器. 安装好 openssl (不会明白怎么安装 op ...
- 20165324 Java实验五 网络编程与安全
20165324 Java实验五 网络编程与安全 一.实验报告封面 课程:Java程序设计 班级:1653班 姓名:何春江 学号:20165324 指导教师:娄嘉鹏 实验日期:2018年5月28日 实 ...
- 爬虫-Beautiful Soup模块
阅读目录 一 介绍 二 基本使用 三 遍历文档树 四 搜索文档树 五 修改文档树 六 总结 一 介绍 Beautiful Soup 是一个可以从HTML或XML文件中提取数据的Python库.它能够通 ...
- Mvc ModelState.isValid为false时,检查时那个字段不符合规则的代码
List<string> sb = new List<string>(); //获取所有错误的Key List<string> Keys = ModelState. ...
- ruby中的instance_eval,class_eval,eval
ruby具有在运行时执行以字符串形式保存的代码的功能设施,eval族方法 .包括Kernel#eval,Object#instance_eval,Module#class_eval. Kernel#e ...
- 47. Permutations II (全排列有重复的元素)
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- POJ - 2125 Destroying The Graph (最小点权覆盖)
题意:给一张图,现在要删去所有的边,删去一个点的所有入边和所有出边都有其对应\(W_{i+}\)和\(W_{i-}\).求删去该图的最小花费,并输出解 分析:简而言之就是用最小权值的点集去覆盖所有的边 ...