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 the two sorted arrays. The overall run time

complexity should be O(log (m+n)).

题目的关键是时间复杂度的要求,不然直接将两个数组凑起来就行,不过为O(m + n);

由于两个数组都已排好序,可以分别取两个数组的中位数进行比较假设分别为A,B,若A < B,显然第一个数组中小与

A的肯定小与整体的中位数(设为C),反之亦然,当然若A
== B,A就是C了。到这里,转换一下思维,何为中位数?

不就是整个数据中处于中间位置的那个数吗(先不分奇偶,意思懂就行),那么中位数也就是正好有一半数据比其小的

那个数,这好像是一个比较经典的问题,即求第K小的数据!想到了这个地方,题目的解法也就出来了,代码如下:

int FindMedium(vector<int>::iterator iter1, int m, vector<int>::iterator iter2, int n, int k)
{
	if(m > n)
		return FindMedium(iter2, n, iter1, m, k);
	if(m == 0)
		return (*(iter2 + k - 1));
	if(k == 1)
		return min(*iter1, *iter2);
	int tmp1 = min(k / 2, m);
	int tmp2 = k - tmp1;
	if(*(iter1 + tmp1 - 1) < *(iter2 + tmp2 - 1))
		return FindMedium(iter1 + tmp1, m - tmp1, iter2, n, tmp2);
	else if (*(iter1 + tmp1 - 1) > *(iter2 + tmp2 - 1))
		return FindMedium(iter1, m, iter2 + tmp2, n - tmp2, tmp1);
	else
		return *(iter1 + tmp1 - 1);
}
class Solution {
public:
	double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2)
	{
		int total = nums1.size() + nums2.size();
		vector<int>::iterator iter1 = nums1.begin();
		vector<int>::iterator iter2 = nums2.begin();
		if(total & 0x01)
			return FindMedium(iter1, nums1.size(), iter2, nums2.size(), total / 2 + 1);
		else
			return ( FindMedium(iter1, nums1.size(), iter2, nums2.size(), total / 2)
			+ FindMedium(iter1, nums1.size(), iter2, nums2.size(), total / 2 + 1) ) * 1.0 / 2;
	}
};

格式调了半天,也没对好,不晓得又出什么问题了,吐槽下CSDN。。。

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-31 13:36:39

LeetCode 4_Median of Two Sorted Arrays的相关文章

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 arrays. The overall run time complexity should be O(log (m+n)). 有两个已排序的数组A和B,大小为m 和 n. 找出两数组的中位数 时间复杂度应该为 O(log (m+n)). 简单粗暴的方法就是把两个数组合并成一个数组,排序,取中位数.

[leetcode]Median of Two Sorted Arrays @ Python

原题地址:https://oj.leetcode.com/problems/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 arrays. The overall run time complexity should be O(log (m+n)). 解题思路:这道题要求两个已经排好

[leetcode] 4. Median of Sorted Arrays

// Question: There are two sorted arrays A and B 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: get the new sorted array, then get the media number. #include

[LeetCode] Median of Two Sorted Arrays [16]

题目 There are two sorted arrays A and B 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)). 原题链接(点我) 解题思路 返回两个排序数组的中位数.这个题可以有以下几个思路: 首先可以想到的是将两个数组merge起来,然后返回其中位数. 第二个是,类似merg

leetcode | Median of Two Sorted Arrays 寻找2个有序数组中第k大的值

问题 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 arrays. The overall run time complexity should be O(log(m + n)). 分析 本题更经典通用的描述方式时: 给定2个有序数组,找出2个数组中所有元素中第k大的元素. 思路1 直观思

[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 sorted arrays. The overall run time complexity should be O(log (m+n)). Subscribe to see which companies asked this question Show Tags 分析: 这个题目在leetco

LeetCode—Median of Two Sorted Arrays

Median of Two Sorted Arrays 这道题要找Median,中位数.这个是指,如果数组大小是偶数,返回中间两个数的平均值,如果是奇数个,就是中间的数. 算法时间效率要求是 O(log(m + n)),具体思路网上都一样. 另外,现在leetCode的C++ 数组都换成vector了,所以只好整理一下vector的用法. 参考:http://imatlab.lofter.com/post/286ffc_a81276 http://www.cnblogs.com/wang7/ar

[JS做LeetCode] Median of Two Sorted Arrays

Median of Two Sorted Arrays <html> <head> <meta http-equiv="charset" content="utf-8"></head> <body> <script> //解题思路: //找出中位数要循环的次数为loop //posA为数组A的下标,posB为数组B的下标 //每次循环找出两个数组对应下标比较小的值,进栈,下标+1 //注意判断数

[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 sorted arrays. The overall run time complexity should be O(log (m+n)). http://www.acmerblog.com/leetcode-median-of-two-sorted-arrays-5330.html http:/