# Leetcode: Median of Two Sorted Arrays

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

• NoMasp
• 2015-09-17 16:13
• 1972

## 【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

• isunn
• 2014-12-04 10:37
• 244

## 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个字)