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));
        }
    }
}
时间: 2024-12-30 18:24:03

LeetCode 4 Median of Two Sorted Arrays的相关文章

c++-leetcode Median of Two Sorted Arrays

问题描述 leetcode Median of Two Sorted Arrays class Solution { public: double findMedianSortedArrays(vector& nums1, vector& nums2) { if(nums1.size()==0){ if(nums2.size()%2==0)return ((double)nums2[nums2.size()/2]+nums2[nums2.size()/2-1])/2.0; else ret

Median of Two Sorted Arrays

There are two sorted arrays A and B 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)).  题解: 首先我们先明确什么是median,即中位数.  引用Wikipedia对中位数的定义: 计算有限个数的数据的中位数的方法是:把所有的同类数据按照大小的顺序排列.如

[LeetCode]82.Remove Duplicates from Sorted List II

[题目] Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list. For example, Given 1->2->3->3->4->4->5, return 1->2->5. Given 1->1->1->2->3, return 2-

[LeetCode]81.Search in Rotated Sorted Array II

[题目] Search in Rotated Sorted Array II  Total Accepted: 3749 Total Submissions: 12937My Submissions Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a f

[LeetCode]33.Search in Rotated Sorted Array

[题目] Search in Rotated Sorted Array  Total Accepted: 5827 Total Submissions: 20925My Submissions Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). You are given a target value

[LeetCode]80.Remove Duplicates from Sorted Array II

[题目] Remove Duplicates from Sorted Array II  Total Accepted: 4460 Total Submissions: 15040My Submissions Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For example, Given sorted array A = [1,1,1,2,2,3], Your fun

LeetCode 26 Remove Duplicates from Sorted Array(从已排序数组中移除重复元素)

翻译 给定一个已排序的数组,删除重复的元素,这样每个元素只出现一次,并且返回新的数组长度. 不允许为另一个数组使用额外的空间,你必须就地以常量空间执行这个操作. 例如, 给定输入数组为 [1,1,2] 你的函数应该返回length = 2, 其前两个元素分别是1和2.它不关心你离开后的新长度. 原文 Given a sorted array, remove the duplicates in place such that each element appear only once and re

LeetCode 33 Search in Rotated Sorted Array(在旋转排序数组中搜索)(*)

翻译 假定一个数组在一个我们预先不知道的轴点旋转. 例如,0 1 2 4 5 6 7可能会变为4 5 6 7 0 1 2. 给你一个目标值去搜索,如果找到了则返回它的索引,否则返回-1. 你可以假定没有重复的元素存在于数组中. 原文 Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). You are

leetcode 26 Remove Duplicates from Sorted Array

Remove Duplicates from Sorted ArrayTotal Accepted: 66627 Total Submissions: 212739 My Submissions                       Given a sorted array, remove the duplicates in place such that each element appear onlyonce and return the new length. Do not allo