[leetcode] 4. Median of Sorted Arrays

// Question: 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)).// Solution: get the new sorted array, then get the media number.

#include <iostream>
#include <string>
using namespace std;

class Solution {
public:
    double findMedianSortedArrays(int A[], int m, int B[], int n)
    {
        int *sorted = (int*)malloc(sizeof(int)*(m + n));
        int ia = 0, ib = 0;
        int i=0;
        double out = 0;
        while(ia<m || ib<n)
        {
            if(ib == n || (ia < m && A[ia] < B[ib]))
            {
                sorted[i] = A[ia];
                ia++;
            }
            else
            {
                sorted[i] = B[ib];
                ib++;
            }
            i++;
        }
        if((m + n) % 2)
        {
            out = sorted[(m+n-1)>>1];
        }
        else
        {
            out = (sorted[(m+n-2)>>1] + sorted[(m+n)>>1]) / 2.;
        }
        free(sorted);
        return out;
    }
};

int main()
{
    int a1[] = {1, 3};
    int b1[] = {2};

    int a2[] = {1};
    int b2[] = {2};

    Solution s;
    cout << s.findMedianSortedArrays(a2, sizeof(a2)/sizeof(int), b2, sizeof(b2)/sizeof(int)) << endl;

    system("pause");
    return 0;
}

[leetcode] 4. Median of Sorted Arrays

时间: 2024-12-27 06:06:00

[leetcode] 4. Median of Sorted Arrays的相关文章

Median of 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)). double findMedian(int A[], int B[], int l, int r, int nA, int nB) { if (l > r) return f

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)). 题目的关键是时间复杂度的要求,不然直接将两个数组凑起来就行,不过为O(m + n): 由于两个数组都已排好序,可以分别取两个数组的中位数进行比较

LeetCode(3) || Median of Two Sorted Arrays

LeetCode(3) || Median of Two Sorted Arrays 题记 之前做了3题,感觉难度一般,没想到突然来了这道比较难的,星期六花了一天的时间才做完,可见以前基础太差了. 题目内容 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

LeetCode——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)). 有两个已排序的数组A和B,大小为m 和 n. 找出两数组的中位数 时间复杂度应该为 O(log (m+n)). 简单粗暴的方法就是把两个数组合并成一个数组,排序,取中位数.

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 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][Python]Median of Two Sorted Arrays

# -*- coding: utf8 -*-'''https://oj.leetcode.com/problems/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)).

【题解】【数组】【Leetcode】Median of Two Sorted Arrays

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)). ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22

Leetcode Array 4 Median of Two Sorted Arrays

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