返回
中位数探秘:巧用二分法求解两个正序数组的中间值
闲谈
2023-10-26 09:40:00
二分法求解中位数算法步骤
- 将两个数组 nums1 和 nums2 合并为一个新的数组 nums。
- 确定新数组 nums 的总长度为 m + n。
- 如果 m + n 为偶数,则中位数为 nums[(m + n) / 2] 和 nums[(m + n) / 2 - 1] 的平均值。
- 如果 m + n 为奇数,则中位数为 nums[(m + n) / 2]。
算法复杂度分析
二分法求解中位数算法的时间复杂度为 O(log(m + n)),其中 m 和 n 分别是两个正序数组 nums1 和 nums2 的长度。
代码示例
Python
def find_median(nums1, nums2):
"""
Finds the median of two sorted arrays.
Args:
nums1: The first sorted array.
nums2: The second sorted array.
Returns:
The median of the two sorted arrays.
"""
# Merge the two arrays into a single sorted array.
nums = sorted(nums1 + nums2)
# Calculate the length of the merged array.
length = len(nums)
# If the length of the merged array is even, the median is the average of the two
# middle elements.
if length % 2 == 0:
median = (nums[length // 2 - 1] + nums[length // 2]) / 2
# Otherwise, the median is the middle element.
else:
median = nums[length // 2]
return median
# Example usage.
nums1 = [1, 3, 5, 7]
nums2 = [2, 4, 6, 8]
median = find_median(nums1, nums2)
print(median) # Output: 4.5
Java
import java.util.Arrays;
class MedianFinder {
public double findMedian(int[] nums1, int[] nums2) {
// Merge the two arrays into a single sorted array.
int[] nums = new int[nums1.length + nums2.length];
System.arraycopy(nums1, 0, nums, 0, nums1.length);
System.arraycopy(nums2, 0, nums, nums1.length, nums2.length);
Arrays.sort(nums);
// Calculate the length of the merged array.
int length = nums.length;
// If the length of the merged array is even, the median is the average of the two
// middle elements.
if (length % 2 == 0) {
return (nums[length / 2 - 1] + nums[length / 2]) / 2.0;
}
// Otherwise, the median is the middle element.
else {
return nums[length / 2];
}
}
// Example usage.
public static void main(String[] args) {
int[] nums1 = {1, 3, 5, 7};
int[] nums2 = {2, 4, 6, 8};
MedianFinder mf = new MedianFinder();
double median = mf.findMedian(nums1, nums2);
System.out.println(median); // Output: 4.5
}
}
C++
#include <iostream>
#include <vector>
using namespace std;
double findMedian(vector<int>& nums1, vector<int>& nums2) {
// Merge the two vectors into a single sorted vector.
vector<int> nums;
nums.insert(nums.end(), nums1.begin(), nums1.end());
nums.insert(nums.end(), nums2.begin(), nums2.end());
sort(nums.begin(), nums.end());
// Calculate the length of the merged vector.
int length = nums.size();
// If the length of the merged vector is even, the median is the average of the two
// middle elements.
if (length % 2 == 0) {
return (nums[length / 2 - 1] + nums[length / 2]) / 2.0;
}
// Otherwise, the median is the middle element.
else {
return nums[length / 2];
}
}
// Example usage.
int main() {
vector<int> nums1 = {1, 3, 5, 7};
vector<int> nums2 = {2, 4, 6, 8};
double median = findMedian(nums1, nums2);
cout << median << endl; // Output: 4.5
return 0;
}
C#
using System;
using System.Collections.Generic;
class MedianFinder
{
public double FindMedian(int[] nums1, int[] nums2)
{
// Merge the two arrays into a single sorted array.
int[] nums = new int[nums1.Length + nums2.Length];
Array.Copy(nums1, 0, nums, 0, nums1.Length);
Array.Copy(nums2, 0, nums, nums1.Length, nums2.Length);
Array.Sort(nums);
// Calculate the length of the merged array.
int length = nums.Length;
// If the length of the merged array is even, the median is the average of the two
// middle elements.
if (length % 2 == 0)
{
return (nums[length / 2 - 1] + nums[length / 2]) / 2.0;
}
// Otherwise, the median is the middle element.
else
{
return nums[length / 2];
}
}
// Example usage.
public static void Main(string[] args)
{
int[] nums1 = { 1, 3, 5, 7 };
int[] nums2 = { 2, 4, 6, 8 };
MedianFinder mf = new MedianFinder();
double median = mf.FindMedian(nums1, nums2);
Console.WriteLine(median); // Output: 4.5
}
}
Javascript
function findMedian(nums1, nums2) {
// Merge the two arrays into a single sorted array.
const nums = nums1.concat(nums2).sort((a, b) => a - b);
// Calculate the length of the merged array.
const length = nums.length;
// If the