Leetcode: Median of Two Sorted Arrays

原创 2016年08月31日 16:27:55
class Solution {
public:
double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
		int num = nums1.size() + nums2.size();
		if (num == 0) {
			return 0.0;
		}
		stack<double>QQ;
		int i = 0, j = 0;
		while (i < nums1.size() && j < nums2.size()) {
		//	cout << "1 2" << endl;
			if (nums1[i] < nums2[j]) {
				QQ.push(nums1[i]);
				i++;
			}
			else {
				QQ.push(nums2[j]);
				j++;
			}
		}
		while (i<nums1.size()) {
		//	cout << "1 " << endl;
			QQ.push(nums1[i]);
			i++;
		}
		while (j < nums2.size()) {
		//	cout << " 2" << endl;
			QQ.push(nums2[j]);
			j++;
		}
		//cout << QQ.top() << endl;
		if (num % 2) {
			int t = num / 2;
			while (t>0) {
				QQ.pop();
				t--;
			}
		}
		else {
			int t = num / 2-1;
			while (t>0) {
				QQ.pop();
				t--;
			}
			double ant = QQ.top();
			QQ.pop();
			ant += QQ.top();
			QQ.pop();
			QQ.push(ant / 2.0);

		}



		return QQ.top();
	}
};

版权声明:本文为博主原创文章,欢迎指教~

相关文章推荐

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 t...
  • fly_yr
  • fly_yr
  • 2015年08月22日 20:35
  • 1054

[leetcode]median of two sorted arrays【寻找第k小的数问题】

Median of Two Sorted ArraysThere are two sorted arrays nums1 and nums2 of size m and n respectively....

Leetcode刷题记—— 4. Median of Two Sorted Arrays(两有序数组的中位数)

一、题目叙述: There are two sorted arrays nums1 and nums2 of size m...

LeetCode (4)Median of Two Sorted Arrays

(4)Median of Two Sorted Arrays题目:两个已经排好序的数组nums1和nums2,大小分别为m和n,求两个已排数组中位数。总运行时间复杂度应该是O(log(m+n))。用例...

Leetcode-median of two sorted arrays

转自:http://blog.csdn.net/yutianzuijin/article/details/11499917/ 这是我做的第二个leetcode题目,一开始以为和第一个一样很简单,但...

leetcode-Median of Two Sorted Arrays

here are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted a...

Leetcode207: Median of Two Sorted Arrays

There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the tw...

[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 sort...

[LeetCode][4]Median of Two Sorted Arrays解析 -Java实现

Q: There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the...

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
举报原因:
原因补充:

(最多只允许输入30个字)