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

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

我是如何成为一名python大咖的?

人生苦短,都说必须python,那么我分享下我是如何从小白成为Python资深开发者的吧。2014年我大学刚毕业..

LeetCode 4 Median of Two Sorted Arrays

翻译有两个给定的排好序的数组nums1和nums2,其大小分别为m和n。 找出这两个已排序数组的中位数。 总运行时间的复杂度应该是O(log(m+n))。原文There are two sorted ...
  • NoMasp
  • NoMasp
  • 2015-09-17 16:13
  • 1972

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

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

Leetcode-median of two sorted arrays

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

【leetcode】4. Median of Two Sorted Arrays

Difficulty:Hard There are two sorted arrays nums1 and nums2 of size m and n respectively. Fi...

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

leetcode之 median of two sorted arrays

出处:http://blog.csdn.net/yutianzuijin/article/details/11499917/ 这是我做的第二个leetcode题目,一开始以为和第一个一样很简...
  • isunn
  • isunn
  • 2014-12-04 10:37
  • 244

LeetCode 4. Median of Two Sorted Arrays

题目描述 There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the media...

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...
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

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