LeetCode 4. Median of Two Sorted Arrays

原创 2018年04月16日 23:06:50

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, 2]
nums2 = [3, 4]

The median is (2 + 3)/2 = 2.5


C/C++

double findMedianSortedArrays(int* nums1, int nums1Size, int* nums2, int nums2Size) {
    double sum = 0.0;
    int total = nums1Size + nums2Size;
    int pos2 = total / 2;
    int pos1 = pos2 - (total%2==0);
    int i = 0,s1 = 0,s2 = 0;
    while(s1<nums1Size&&s2<nums2Size&&i<=pos2)
    {
        int tmp = nums1[s1]<nums2[s2]?nums1[s1++]:nums2[s2++];
        if(i>=pos1)sum+=tmp;
        ++i;
    }
    while(s1<nums1Size&&i<=pos2)
    {
        if(i>=pos1)sum+=nums1[s1];
        ++s1;
        ++i;
    }
    while(s2<nums2Size&&i<=pos2)
    {
        if(i>=pos1)sum+=nums2[s2];
        ++s2;
        ++i;
    }
    return pos1==pos2?sum:sum*5/10;
}

Java

class Solution {
    public double findMedianSortedArrays(int[] nums1, int[] nums2) {
        double sum = 0.0;
    int nums1Size = nums1.length;
    int nums2Size = nums2.length;
    int total = nums1Size + nums2Size;
    int pos2 = total / 2;
    int pos1 = pos2 - (total%2==0?1:0);
    int i = 0,s1 = 0,s2 = 0;
    while(s1<nums1Size&&s2<nums2Size&&i<=pos2)
    {
        int tmp = nums1[s1]<nums2[s2]?nums1[s1++]:nums2[s2++];
        if(i>=pos1)sum+=tmp;
        ++i;
    }
    while(s1<nums1Size&&i<=pos2)
    {
        if(i>=pos1)sum+=nums1[s1];
        ++s1;
        ++i;
    }
    while(s2<nums2Size&&i<=pos2)
    {
        if(i>=pos1)sum+=nums2[s2];
        ++s2;
        ++i;
    }
    return pos1==pos2?sum:sum*5/10;
    }
}

Python

class Solution:
    def findMedianSortedArrays(self, nums1, nums2):
        """
        :type nums1: List[int]
        :type nums2: List[int]
        :rtype: float
        """
        s = 0.0
        nums1Size = len(nums1)
        nums2Size = len(nums2)
        total = nums1Size + nums2Size
        pos2 = total // 2
        pos1 = pos2-1 if total%2==0 else pos2
        i,s1,s2 = 0,0,0
        while s1<nums1Size and s2<nums2Size and i<=pos2:
            tmp = nums1[s1]
            if nums1[s1]>nums2[s2]:
                tmp = nums2[s2]
                s2 += 1
            else: s1 += 1
            if i>=pos1:s += tmp
            i += 1
        while s1<nums1Size and i<=pos2:
            if i>=pos1:s += nums1[s1]
            s1 += 1
            i += 1
        while s2<nums2Size and i<=pos2:
            if i>=pos1:s += nums2[s2]
            s2 += 1
            i += 1
        return s if pos1==pos2 else s*5/10

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_39008204/article/details/79968239

leetcode: 4. Median of Two Sorted Arrays (java)

题目链接:https://leetcode.com/problems/median-of-two-sorted-arrays/ 题目: There are two sorted arrays nu...
  • j754379117
  • j754379117
  • 2016-06-20 20:44:18
  • 737

leetcode之 median of two sorted arrays

这是我做的第二个leetcode题目,一开始以为和第一个一样很简单,但是做的过程中才发现这个题目非常难,给人一种“刚上战场就踩上地雷挂掉了”的感觉。后来搜了一下leetcode的难度分布表(leetc...
  • yutianzuijin
  • yutianzuijin
  • 2013-09-10 00:15:50
  • 80226

《LeetBook》leetcode题解(4): Median of Two Sorted Arrays[H]——两个有序数组中值问题

004. Median of Two Sorted Arrays[H]Median of Two Sorted ArraysH 题目 分析 思路1 思路2 预备知识 先解释下割 双数组找第k个元素 我...
  • hk2291976
  • hk2291976
  • 2016-04-09 21:24:06
  • 4829

leetcode04-Median of Two Sorted Arrays-python

There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two...
  • summerdj
  • summerdj
  • 2016-03-31 15:11:13
  • 898

两个有序数组的中位数Median of Two Sorted Arrays(很重要)

https://leetcode.com/problems/median-of-two-sorted-arrays/ 对于一个长度为n的已排序数列a,若n为奇数,中位数为a[n / 2 + 1]...
  • gao1440156051
  • gao1440156051
  • 2016-06-21 11:11:03
  • 4842

[LeetCode] 004. Median of Two Sorted Arrays (Hard) (C++/Java/Python)

[LeetCode] 004. Median of Two Sorted Arrays (Hard) (C++/Java/Python)
  • hcbbt
  • hcbbt
  • 2015-02-28 00:03:30
  • 5727

LeetCode 4. Median of Two Sorted Arrays(两个有序数组的中位数)

原题网址:https://leetcode.com/problems/median-of-two-sorted-arrays/ There are two sorted arrays nums1 an...
  • jmspan
  • jmspan
  • 2016-05-01 15:39:34
  • 618

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 t...
  • fly_yr
  • fly_yr
  • 2015-08-22 20:35:01
  • 1333

leetcode 4. Median of Two Sorted Arrays O(log(m+n))解法

4. Median of Two Sorted Arrays   Question Editorial Solution  My Submissions ...
  • yuxin6866
  • yuxin6866
  • 2016-09-03 20:21:42
  • 893

Median of Two Sorted Arrays 两个有序数组的中位数@LeetCode

超级难的一道题,线性时间复杂度好做,就是merge。 但是对数复杂度,就要用到很多数学分析,实际上就是要找到第k小的元素。 翻遍了网络,觉得还是这一篇讲的最详细,而且写得代码最容易转为Java,因为J...
  • hellobinfeng
  • hellobinfeng
  • 2013-12-16 13:52:04
  • 4735
收藏助手
不良信息举报
您举报文章:LeetCode 4. Median of Two Sorted Arrays
举报原因:
原因补充:

(最多只允许输入30个字)