leetcode---median-of-two-sorted-arrays---链表

来源:互联网 发布:吴彦祖 知乎 编辑:程序博客网 时间:2024/06/14 17:32

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

class Solution {public:    double findMedianSortedArrays(int A[], int m, int B[], int n)     {        if(!m && !n)            return 0;        int *a = new int[m+n];        memcpy(a, A, sizeof(int)*m);        memcpy(a+m, B, sizeof(int)*n);        sort(a, a+m+n);        int k = m + n;        double mid =  k % 2 == 1 ? a[k>>1] : (a[k>>1] + a[(k-1)>>1])/2.0;        delete []a;        return mid;    }};
原创粉丝点击