LeetCode 4: Median of Two Sorted Arrays

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))

代码如下:

class Solution {

 int getMedian(int A[], int m, int B[], int n,int k)
    {
        if(m>n)
            return getMedian(B,n,A,m, k);
        if(m ==0)
            return B[k-1];
        if(k==1)
            return min(A[0], B[0]);
        int pa = min(k/2, m);
        int pb = k - pa;
        if (A[pa-1] < B[pb-1]) {
            return getMedian(A+pa, m-pa, B, n, k-pa);
        } else if(A[pa-1] > B[pb-1]) {
            return getMedian(A, m, B+pb, n-pb, k-pb);
        }else{
            return A[pa-1];
        }

        return 0;

    }
   public: double findMedianSortedArrays(int A[], int m, int B[], int n) {
        if ((m+n)%2 == 0) {
            return (getMedian(A, m,B, n, (m+n)/2) + getMedian(A, m,B, n, (m+n)/2+1)) /2.0;
        } else {
            return getMedian(A, m,B, n, (m+n)/2+1);
        }

    }
};
时间: 2024-10-10 21:15:45

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

LeetCode2: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)). 解题思路: 这道题,很容易想到的是先排序再直接定位中间那个数即可,m+n为偶数的话,应为中间两数之和除2,但时间复杂度不符合题目要求,还一种方法就是利用归并思想,因

【LeetCode OJ】Median of Two Sorted Arrays

题目链接:https://leetcode.com/problems/median-of-two-sorted-arrays/ 题目: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)). 解题思路:将两个有序数

Leetcode Array 4 Median of Two Sorted Arrays

做leetcode题目的第二天,我是按照分类来做的,做的第一类是Array类,碰见的第二道题目,也就是今天做的这个,题目难度为hard.题目不难理解,但是要求到了时间复杂度,就需要好好考虑使用一下算法了.刚开始没啥思路,就用暴力的方法,用双层循环遍历的一下两个已经排好序的数组 ,在中间位置停止找道中位数.这样时间复杂度是肯定不能满足题目要求的,但是程序测试还是过了. 苦于自己没有思路,又不甘心就这样水过一道题,还是搜了一下博客,膜拜了一下大神.最好的方法是将中位数  -- 两个数组数据排好序之后

No.004:Median of Two Sorted Arrays

题目: 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)). Example1:nums1 = [1, 3]nums2 = [2]The median is 2.0Example2:nums1 = [1, 2]n

【Leetcode】4. Median of Two Sorted Arrays

题目描述: 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)). 解题思路: 这道题个人觉得是很难的,要想解出这个题,就必须了解一下几点: 1.求一列长度为len的数字的中位数,其实也可以被看做是求第len/2

Q4:Median of Two Sorted Arrays

4. Median of Two Sorted Arrays 官方的链接:4. Median of Two Sorted Arrays Description : 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

leetcode.C.4. Median of Two Sorted Arrays

4. Median of Two Sorted Arrays 这应该是最简单最慢的方法了,因为本身为有序,所以比较后排序再得到中位数. 1 double findMedianSortedArrays(int* nums1, int nums1Size, int* nums2, int nums2Size) { 2 int numsSize = nums1Size + nums2Size; 3 int *nums = (int*)malloc(sizeof(int)*numsSize); 4 do

leetcode -- Algorithms -- 4_ Median of Two Sorted Arrays

00 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)). class Solution(object): def findMedianSortedArrays(self, nums1, nums2): new

LeetCode Problem 4.Median of Two Sorted Arrays

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)). Example 1: nums1 = [1, 3] nums2 = [2] The median is 2.0 Example 2: nums1 = [1,