码迷,mamicode.com
首页 > 其他好文 > 详细

Median of Two Sorted Arrays

时间:2015-05-07 14:34:14      阅读:87      评论:0      收藏:0      [点我收藏+]

标签:c++   程序员   面试   查找   

题目: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)).


分析:

以下思路来自脑客爱刷题。时间复杂度是O(log(min(m,n))).


class Solution {
public:
	double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
		if (nums1.empty() && nums2.empty())
			throw exception();

		if (nums1.size()>nums2.size())
			return findMedianSortedArrays(nums2, nums1);
		int lens = nums1.size(), lenl = nums2.size();
		if (lens == lenl)
		{
			double r1 = MedianEqualdArrays(nums1, 0, lens - 1, nums2, 0, lenl - 1);
			double r2 = MedianEqualdArrays_down(nums1, 0, lens - 1, nums2, 0, lenl - 1);
			return (r1 + r2) / 2;
		}
		else if ((lens + lenl) & 1)
			return find_Kth_number(nums1, nums2, (lens + lenl) / 2 + 1);
		else
		{
			int index1 = (lens + lenl) / 2, index2 = index1 + 1;
			double r1 = find_Kth_number(nums1, nums2, index1), r2 = find_Kth_number(nums1, nums2, index2);
			return (r1+r2)/2;
		}
		
	}

	double find_Kth_number(vector<int>& nums1, vector<int>& nums2, int index)
	{
		if (nums1.empty())
		{
			return nums2[index - 1];
		}
		if (nums2.empty())
		{
			return nums1[index - 1];
		}

		int lens = nums1.size(), lenl = nums2.size();
		if (index <= lens)
		{
			return MedianEqualdArrays(nums1, 0, index - 1, nums2, 0, index - 1);
		}
		else if (index>lenl)
		{
			int offset = lens + lenl - index, index1 = lens - offset, index2 = lenl - offset;
			return MedianEqualdArrays(nums1, index1 - 1, lens - 1, nums2, index2 - 1, lenl - 1);
		}
		else// (lens,lensl]
		{
			int nums2_begin = index - lens - 1, nums_end = index - 1;
			if (nums2[nums2_begin] >= nums1.back())
				return nums2[nums2_begin];
			else
			{
				return MedianEqualdArrays(nums1, 0, lens - 1, nums2, nums2_begin + 1, nums_end);
			}
		}
	}

	//返回的是比较小的那个中位数
	double MedianEqualdArrays(vector<int>& nums1, int begin1, int end1, vector<int>& nums2, int begin2, int end2)
	{
		int len = end1 - begin1 + 1;
		if (len == 1)
		{
			return min(nums1[begin1], nums2[begin2]);
		}
		else
		{
			int move_step = len / 2;
			if ((len & 1) == 0)
			{
				--move_step;
			}
			int mid1 = begin1 + move_step, mid2 = begin2 + move_step;
			if (nums1[mid1] == nums2[mid2])
				return nums1[mid1];
			else if (nums1[mid1]<nums2[mid2])
			{
				if (len & 1)
					return MedianEqualdArrays(nums1, mid1, end1, nums2, begin2, mid2);
				else
					return MedianEqualdArrays(nums1, mid1 + 1, end1, nums2, begin2, mid2);
			}
			else//nums1[mid1]>nums2[mid2]
			{
				if (len & 1)
					return MedianEqualdArrays(nums1, begin1, mid1, nums2, mid2, end2);
				else
					return MedianEqualdArrays(nums1, begin1, mid1, nums2, mid2 + 1, end2);
			}
		}
	}
	//返回的是比较大的那个中位数
	double MedianEqualdArrays_down(vector<int>& nums1, int begin1, int end1, vector<int>& nums2, int begin2, int end2)
	{
		int len = end1 - begin1 + 1;
		if (len == 1)
		{
			return max(nums1[begin1], nums2[begin2]);
		}
		else
		{
			int move_step = len / 2;
			int mid1 = begin1 + move_step, mid2 = begin2 + move_step;
			if (nums1[mid1] == nums2[mid2])
				return nums1[mid1];
			else if (nums1[mid1]<nums2[mid2])
			{
				if (len & 1)
					return MedianEqualdArrays_down(nums1, mid1, end1, nums2, begin2, mid2);
				else
					return MedianEqualdArrays_down(nums1, mid1, end1, nums2, begin2, mid2 - 1);
			}
			else//nums1[mid1]>nums2[mid2]
			{
				if (len & 1)
					return MedianEqualdArrays_down(nums1, begin1, mid1, nums2, mid2, end2);
				else
					return MedianEqualdArrays_down(nums1, begin1, mid1 - 1, nums2, mid2, end2);
			}
		}
	}
};


Median of Two Sorted Arrays

标签:c++   程序员   面试   查找   

原文地址:http://blog.csdn.net/bupt8846/article/details/45559877

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!