Given two sorted arrays nums1
and nums2
of size m
and n
respectively, return the median of the two sorted arrays.
The overall run time complexity should be O(log (m+n))
.
Example 1:
Input: nums1 = [1,3], nums2 = [2] Output: 2.00000 Explanation: merged array = [1,2,3] and median is 2.
Example 2:
Input: nums1 = [1,2], nums2 = [3,4] Output: 2.50000 Explanation: merged array = [1,2,3,4] and median is (2 + 3) / 2 = 2.5.
JAVA
class Solution { public double findMedianSortedArrays(int[] nums1, int[] nums2) { int index1 = 0; int index2 = 0; int med1 = 0; int med2 = 0; for (int i=0; i<=(nums1.length+nums2.length)/2; i++) { med1 = med2; if (index1 == nums1.length) { med2 = nums2[index2]; index2++; } else if (index2 == nums2.length) { med2 = nums1[index1]; index1++; } else if (nums1[index1] < nums2[index2] ) { med2 = nums1[index1]; index1++; } else { med2 = nums2[index2]; index2++; } } // the median is the average of two numbers if ((nums1.length+nums2.length)%2 == 0) { return (float)(med1+med2)/2; }
}
JAVASCRIPT
var findMedianSortedArrays = function (nums1, nums2) {
if (nums1.length == 0 || nums2.length == 0) {
if ((nums1.length + nums2.length) % 2 == 1) {
const index = parseInt((nums1.length + nums2.length) / 2);
return nums2.length == 0 ? nums1[index] : nums2[index];
} else {
let nums = nums2.length == 0 ? nums1 : nums2;
const index = nums.length / 2;
return (nums[index - 1] + nums[index]) / 2;
}
}
if (nums1.length > nums2.length) {
swap(nums1, nums2);
}
const M = nums1.length,
N = nums2.length;
let min = 0,
max = M,
half = parseInt((M + N + 1) / 2); // 连个数组合并的中间值
while (min <= max) {
let i = parseInt((min + max) / 2); // nums1 的索引值
let j = half - i; // num2 的索引值
if (i < max && nums2[j - 1] > nums1[i]) {
min++;
} else if (i > min && nums1[i - 1] > nums2[j]) {
max--;
} else {
let maxLeft = 0;
if (i == 0) {
maxLeft = nums2[j - 1];
} else if (j == 0) {
maxLeft = nums1[i - 1];
} else {
maxLeft = Math.max(nums1[i - 1], nums2[j - 1]);
}
if ((M + N) % 2 == 1) {
return maxLeft;
}
let minRight = 0;
if (i == M) {
minRight = nums2[j];
} else if (j == N) {
minRight = nums1[i];
} else {
minRight = Math.min(nums1[i], nums2[j]);
}
return (maxLeft + minRight) / 2;
}
}
return 0;
};
function swap(a, b) {
let tmp = a;
a = b;
b = tmp;
}
const nums1 = [4, 5];
const nums2 = [1, 2, 3];
findMedianSortedArrays(nums1, nums2);
PYTHON
class Solution:
def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float:
def findKth(i: int, j: int, k: int) -> float:
if i >= m:
return nums2[j + k - 1]
if j >= n:
return nums1[i + k - 1]
if k == 1:
return min(nums1[i], nums2[j])
# Division a // b : floordiv(a, b)
midVal1 = nums1[i + k // 2 - 1] if i + k // 2 - 1 < m else math.inf
midVal2 = nums2[j + k // 2 - 1] if j + k // 2 - 1 < n else math.inf
if midVal1 < midVal2:
return findKth(i + k // 2, j, k - k // 2)
else:
return findKth(i, j + k // 2, k - k // 2)
m = len(nums1)
n = len(nums2)
# Division a // b : floordiv(a, b)
left = (m + n + 1) // 2
right = (m + n + 2) // 2
return (findKth(0, 0, left) + findKth(0, 0, right)) / 2.0
############
class Solution(object):
def findMedianSortedArrays(self, nums1, nums2):
a, b = sorted((nums1, nums2), key=len)
m, n = len(a), len(b)
after = (m + n - 1) / 2
lo, hi = 0, m
while lo < hi:
i = (lo + hi) / 2
if after - i - 1 < 0 or a[i] >= b[after - i - 1]:
hi = i
else:
lo = i + 1
i = lo
nextfew = sorted(a[i:i + 2] + b[after - i:after - i + 2])
return (nextfew[0] + nextfew[1 - (m + n) % 2]) / 2.0
C++
class Solution { public: double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) { if (nums1.size() > nums2.size()) swap(nums1, nums2); int M = nums1.size(), N = nums2.size(), L = 0, R = M, K = (M + N + 1) / 2; while (true) { int i = (L + R) / 2, j = K - i; if (i < M && nums2[j - 1] > nums1[i]) L = i + 1; else if (i > L && nums1[i - 1] > nums2[j]) R = i - 1; else { int maxLeft = max(i ? nums1[i - 1] : INT_MIN, j ? nums2[j - 1] : INT_MIN); if ((M + N) % 2) return maxLeft; int minRight = min(i == M ? INT_MAX : nums1[i], j == N ? INT_MAX : nums2[j]); return (maxLeft + minRight) / 2.0; } } } };