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
最新推荐文章于 2021-03-09 19:39:31 发布