LeetCode #4 Median of Two Sorted Arrays C# Solution

本文介绍了一个高效的算法来解决LeetCode上的第4题——寻找两个有序数组的中位数。该算法通过递归查找的方式实现了O(log(m+n))的时间复杂度,并详细讨论了边界条件的处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

LeetCode #4 Problem
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)).

这道题应该是在算法导论上出现过,不过算法导论上是两个长度相同的有序数组,而这里变成了两个长度不定甚至可以为空的数组。这增大了很多判断的条件。
具体算法最开始想到的是两个数组归并排序直到排到中位数的下标,这样的时间复杂度是O(m+n),然而题目要求是O(log(m+n))时间复杂度的算法。
然后想到log一般是二分类题目常见的时间复杂度,所以可以这么想,对于A[]和B[],如果A[MID] == B[MID],那么,显然这个就是中位数。对于其他情况,如果A.Length < B.Length,那么交换A和B。如果A[MID] > B[MID],那么可以显然中位数不会存在在A[0…MID]和B[MID…B.Length]的区间中,这样理想情况下一次就可以将规模减半,然后递归查找,终止条件就是A只剩下一个元素。
这个题最重要的就是细节的处理,小数据尤其是空数组的情况需要判断。
参考:http://blog.youkuaiyun.com/zxzxy1988/article/details/8587244

C# Code
   public class Solution
    {
        int min(int a,int b)
        {
            if (a > b) return b; else return a;
        }
        double findKth(int[] a, int[] b, int k)
        {
            int m = a.Length;
            int n = b.Length;
            if (m > n) return findKth(b, a, k);
            if (m == 0) return b[k - 1];
            if (k == 1) return min(a[0], b[0]);
            int pa = min(k / 2, m), pb = k - pa;
            if (a[pa - 1] < b[pb - 1])
            {
                int[] temp = new int[a.Length];
                Array.Copy(a, pa, temp, 0, a.Length - pa);
                return findKth(temp, b, k - pa);
            }
            else if (a[pa - 1] > b[pb - 1])
            {
                int[] temp = new int[b.Length];
                Array.Copy(b, pb, temp, 0, b.Length - pb);
                return findKth(a, temp, k - pb);
            }
            else
                return a[pa - 1];
        }
        public double FindMedianSortedArrays(int[] nums1, int[] nums2)
        {
            int m = nums1.Length, n = nums2.Length, total = m + n;

            if (m == 1 && n == 0) return (nums1[0]);
            if (m == 0 && n == 1) return (nums2[0]);
            if (total<10000)
            {
                int[] temp = new int[total];
                nums1.CopyTo(temp, 0);
                nums2.CopyTo(temp, m);
                Array.Sort(temp);
                if ((total % 2) == 1)
                {
                    return temp[temp.Length / 2];
                }
                else return ((temp[temp.Length / 2] + temp[temp.Length / 2 - 1]) / 2.0);
            }
            if (m == 1 && n == 1) return ((nums1[0] + nums2[0]) / 2.0);
            if ((total % 2) == 1)
                return findKth(nums1, nums2, total / 2 + 1);
            else
                return (findKth(nums1, nums2, total / 2)
                        + findKth(nums1, nums2, total / 2 + 1)) / 2;
        }
    };

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值