leetcode-algorithms-4 Median of Two Sorted Arrays
leetcode-algorithms-4 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 sorted arrays. The overall run time complexity should be O(log (m+n)).
You may assume nums1 and nums2 cannot be both empty.
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
解法
left | right
a[0] ... a[i-1] | a[i] ... a[m]
b[0] ... b[j-1] | b[j] ... b[n]
对于两个数组,如上所示,保证max(left) <= min(right)同时左边数的个数为中位数个数即(m+n+1)/2.就可以找到中位数的值.
可先取a数组i = m/2,那j = mid(中位数) - i;如果a[i - 1]大于b[j]不能保证max(left) <= min(right)就需要把a的扣掉一个,b[j - 1]和a[i]也是一样的情况.当上面两种都不符合时,就说明已经找到中位数的位置.
class Solution
{
public:
double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2)
{
int l1 = nums1.size();
int l2 = nums2.size();
int l = l1 + l2;
if (l1 > l2)
{
nums1.swap(nums2);
l1 = l - l1;
l2 = l - l1;
}
int mid = (l + 1) / 2;
int min = 0;
int max = l1;
while(min <= max)
{
int i = (min + max) / 2;
int j = mid - i;
if (i < max && nums2[j - 1] > nums1[i])
min = i + 1;
else if (i > min && nums1[i - 1] > nums2[j])
max = i - 1;
else
{
int left = 0;
if (i == 0)
left = nums2[j - 1];
else if (j == 0)
left = nums1[i - 1];
else
left = nums2[j - 1] > nums1[i - 1] ? nums2[j - 1] : nums1[i - 1];
if (l % 2 != 0)
return left;
int right = 0;
if (i == l1)
right = nums2[j];
else if (j == l2)
right = nums1[i];
else
right = nums2[j] > nums1[i] ? nums1[i] : nums2[j];
return double(left + right) / 2.0;
}
}
return 0.0;
}
};
时间复杂度: O(log(m+n)).
空间复杂度: O(1).
leetcode-algorithms-4 Median of Two Sorted Arrays的更多相关文章
- 【LeetCode】4. Median of Two Sorted Arrays (2 solutions)
Median of Two Sorted Arrays There are two sorted arrays A and B of size m and n respectively. Find t ...
- 《LeetBook》leetcode题解(4): Median of Two Sorted Arrays[H]——两个有序数组中值问题
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- leetcode.C.4. Median of Two Sorted Arrays
4. Median of Two Sorted Arrays 这应该是最简单最慢的方法了,因为本身为有序,所以比较后排序再得到中位数. double findMedianSortedArrays(in ...
- 【LeetCode】4. Median of Two Sorted Arrays 寻找两个正序数组的中位数
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:数组,中位数,题解,leetcode, 力扣,python ...
- leetcode第二题--Median of Two Sorted Arrays
Problem:There are two sorted arrays A and B of size m and n respectively. Find the median of the two ...
- 【一天一道LeetCode】#4 Median of Two Sorted Arrays
一天一道LeetCode (一)题目 There are two sorted arrays nums1 and nums2 of size m and n respectively. Find th ...
- 【LeetCode OJ】Median of Two Sorted Arrays
题目链接:https://leetcode.com/problems/median-of-two-sorted-arrays/ 题目:There are two sorted arrays nums1 ...
- Leetcode Array 4 Median of Two Sorted Arrays
做leetcode题目的第二天,我是按照分类来做的,做的第一类是Array类,碰见的第二道题目,也就是今天做的这个,题目难度为hard.题目不难理解,但是要求到了时间复杂度,就需要好好考虑使用一下算法 ...
- 【leetcode】4. Median of Two Sorted Arrays
题目描述: There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of t ...
- LeetCode OJ 4. 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 ...
随机推荐
- (转载)C#控件缩写规范
标准控件缩写规范 类 型 前 缀 示 例 Adrotator adrt adrtTopAd BulletedList blst blstCity Button btn btnSubmit Calend ...
- 《操作系统_FCFS和SJF》
先来先服务FCFS和短作业优先SJF进程调度 转自:https://blog.csdn.net/qq_34374664/article/details/73231072 一.概念介绍和案例解析 FCF ...
- mysql中between...and..的使用,及时间范围的查询
博主原创,转载注明出处: 在mysql应用中,以范围进行查询的很多是以时间范围作为条件进行范围查询的,时间范围查询有 很多种写法,首先总结一下between....and...的使用方法: <s ...
- Latex 左右引号
参考: LaTeX技巧218:LaTeX如何正确输入引号:双引号""单引号'' Latex 左右引号 在latex中加引号时,使用""的输出为两个同向的引号: ...
- dhcp、tftp及pxe简介
DHCP: 全称:Dynamic Host Configuration Protocol 动态主机配置协议 DHCP配置内容: IP/Netmask Gateway DNS Server bootp ...
- Leetcode167-Two Sum II Input array is sorted-Easy
Given an array of integers that is already sorted in ascending order, find two numbers such that the ...
- hdu 5795 A Simple Nim 博弈sg函数
A Simple Nim Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Pro ...
- python中的print()、str()和repr()的区别
print()函数,我们可以看出,在Python IDLE中直接输入的字符串都是有类型的,而print打印后的字符串相当于一串文字,把字符串的引号也省略了,没有类型 print()函数,生成可读性更好 ...
- ros 充电topic
#!/usr/bin/env python #coding=utf- import rospy from std_msgs.msg import String i= def talker(): glo ...
- CentOS7 下更改源
在CentOS 7下更改yum源与更新系统. [1] 首先备份/etc/yum.repos.d/CentOS-Base.repo cp /etc/yum.repos.d/CentOS-Base.rep ...