LeetCode 4 Median of Two Sorted Arrays

简介:

翻译

有两个给定的排好序的数组nums1和nums2,其大小分别为m和n。
找出这两个已排序数组的中位数。
总运行时间的复杂度应该是O(log(m+n))

原文

There are two sorted arrays nums1 and nums2 of size m and n respectively.
Find the median of the two sorted arrays. 
The overall run time complexity should be O(log (m+n)).

C#

public class Solution {
    public double FindMedianSortedArrays(int[] nums1, int[] nums2) {
        int len1=nums1.Length;
        int len2=nums2.Length;
        bool isEven=(nums1.Length+nums2.Length)%2==0;

        int left=(len1+len2+1)/2;
        int right=(len1+len2+2)/2;

        if (isEven)
        {
            var leftValue = findKth(nums1, 0, len1 - 1, nums2, 0, len2 - 1, left);
            var rightValue = findKth(nums1, 0, len1 - 1, nums2, 0, len2 - 1, right);
            return (leftValue + rightValue) / 2.0;
        }
        else
        {
            return findKth(nums1, 0, len1 - 1, nums2, 0, len2 - 1, right);
        }
    }
    public double findKth(int[] A,int lowA,int highA,int[] B,int lowB,int highB,int k)
    {
        if(lowA>highA)
        {
            return B[lowB+k-1];
        }
        if(lowB>highB)
        {
            return A[lowA+k-1];
        }
        int midA=(lowA+highA)/2;
        int midB=(lowB+highB)/2;
        if (A[midA] <= B[midB])
        {
            return k <= midA - lowA + midB - lowB + 1 ?
                this.findKth(A, lowA, highA, B, lowB, midB - 1, k) :
                this.findKth(A, midA + 1, highA, B, lowB, highB, k - (midA - lowA + 1));
        }
        else
        {
            return k <= midA - lowA + midB - lowB + 1 ?
                this.findKth(A, lowA, midA - 1, B, lowB, highB, k) : 
                this.findKth(A, lowA, highA, B, midB + 1, highB, k - (midB - lowB + 1));
        }
    }
}
目录
相关文章
|
5月前
Leetcode 4. Median of Two Sorted Arrays
题目描述很简单,就是找到两个有序数组合并后的中位数,要求时间复杂度O(log (m+n))。 如果不要去时间复杂度,很容易就想到了归并排序,归并排序的时间复杂度是O(m+n),空间复杂度也是O(m+n),不满足题目要求,其实我开始也不知道怎么做,后来看了别人的博客才知道有个二分法求两个有序数组中第k大数的方法。
16 0
|
5月前
Leetcode Find Minimum in Rotated Sorted Array 题解
对一个有序数组翻转, 就是随机取前K个数,移动到数组的后面,然后让你找出最小的那个数,注意,K有可能是0,也就是没有翻转。
16 0
LeetCode 167 Two Sum II - Input array is sorted(输入已排序数组,求其中两个数的和等于给定的数)
给定一个有序数组和一个目标值 找出数组中两个成员,两者之和为目标值,并顺序输出
58 0
|
算法 测试技术
LeetCode 88. 合并两个有序数组 Merge Sorted Array
LeetCode 88. 合并两个有序数组 Merge Sorted Array
LeetCode 21. 合并两个有序链表 Merge Two Sorted Lists
LeetCode 21. 合并两个有序链表 Merge Two Sorted Lists
|
存储 算法
LeetCode 350. 两个数组的交集 II ntersection of Two Arrays II
LeetCode 350. 两个数组的交集 II ntersection of Two Arrays II
LeetCode 350. Intersection of Two Arrays II
给定两个数组,编写一个函数来计算它们的交集。
46 0
LeetCode 350. Intersection of Two Arrays II
|
Python
LeetCode 349. Intersection of Two Arrays
给定两个数组,编写一个函数来计算它们的交集。
51 0
LeetCode 349. Intersection of Two Arrays
|
算法
LeetCode Find Minimum in Rotated Sorted Array II
假设按照升序排序的数组在预先未知的某个点上进行了旋转。 ( 例如,数组 [0,1,2,4,5,6,7] 可能变为 [4,5,6,7,0,1,2] )。 请找出其中最小的元素。 注意数组中可能存在重复的元素。
63 0
LeetCode Find Minimum in Rotated Sorted Array II
LeetCode 153. Find Minimum in Rotated Sorted Array
假设按照升序排序的数组在预先未知的某个点上进行了旋转。 ( 例如,数组 [0,1,2,4,5,6,7] 可能变为 [4,5,6,7,0,1,2] )。 请找出其中最小的元素。 你可以假设数组中不存在重复元素。
78 0
LeetCode 153. Find Minimum in Rotated Sorted Array