solution.cpp 639 字节
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
class Solution
{
public:
	double findMedianSortedArrays(vector<int> &nums1, vector<int> &nums2)
	{
		int nums1Size = nums1.size();
		int nums2Size = nums2.size();
		int na = nums1Size + nums2Size;
		int *ns = (int *)malloc(4 * na);
		int i = 0, j = 0, d = 0;
		int m = na / 2 + 1;
		while (d < m)
		{
			int n;
			if (i < nums1Size && j < nums2Size)
			{
				n = (nums1[i] < nums2[j]) ? nums1[i++] : nums2[j++];
			}
			else if (i < nums1Size)
			{
				n = nums1[i++];
			}
			else if (j < nums2Size)
			{
				n = nums2[j++];
			}
			ns[d++] = n;
		}
		if (na % 2)
		{
			return ns[d - 1];
		}
		return (ns[d - 1] + ns[d - 2]) / 2.0;
	}
};