← 返回 rippling 的题目列表Median of Two Sorted Arrays
类型:qbank
Given two sorted arrays nums1 and nums2 of sizes m and n, return the median of the combined arrays in O(log(m+n)) time. This is the classic LeetCode-hard problem: the intended solution binary-searches for the correct partition of the shorter array rather than merging linearly.
Median of Two Sorted Arrays
Given two sorted arrays nums1 and nums2 of sizes m and n, return the median of the combined arrays in O(log(m+n)) time. This is the classic LeetCode-hard problem: the intended solution binary-searches for the correct partition of the shorter array rather than merging linearly.
SWE
array
binary-search
divide-and-conquer
hard
Frequency
Low
Last asked
2026-02-01
Stage
oa · phone-screen
Median of Two Sorted Arrays
Problem Statement
You are given two sorted arrays, nums1 and nums2. The size of the first array is m and the size of the second array is n. Your task is to find the median value of these two arrays.
You must design a solution that is very fast. The time complexity must be O(log(m+n)).
Sample Cases
Case 1
Input: nums1 = [1,3], nums2 = [2]
Output: 2.00000
Why: If you merge the arrays, you get [1, 2, 3]. The median is 2.
Case 2
Input: nums1 = [1,2], nums2 = [3,4]
Output: 2.50000
Why: If you merge the arrays, you get [1, 2, 3, 4]. The median is the average of 2 and 3, which is 2.5.
Input Limits
nums1 has a length of m.
nums2 has a length of n.
m and n are between 0 and 1000.
The total size (m + n) is at least 1 and no more than 2000.
The values inside the arrays range from -1,000,000 to 1,000,000.