标签:style blog http color 使用 os io for
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) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int *a=new int[m+n];
memcpy(a,A,sizeof(int)*m);
memcpy(a+m,B,sizeof(int)*n);
sort(a,a+n+m);
double median=(double) ((n+m)%2? a[(n+m)>>1]:(a[(n+m-1)>>1]+a[(n+m)>>1])/2.0);
delete a;
return median;
}
};
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class Solution
{
private:
double findKth(int a[], int m, int b[], int n, int k)
{
int i = 0;
int j = 0;
int index = 1;
int kth;
if (m == 0)
return b[k-1];
if (n == 0)
return a[k-1];
if (k ==1)
return a[0]>b[0] ? b[0] : a[0];
if (k == 2)
return (a[0] + b[0]) /2.0;
while(index <= k && i < m && j < n)
{
if( a[i] >= b[j])
{
index ++;
kth = b[j];
j ++;
}
else
{
index ++;
kth = a[i];
i ++;
}
}
if( index < k && j == n)
{
kth = a[i+k-index];
}
if (index < k && i == m)
kth = b[j+k-index];
return kth;
}
public:
double findMedianSortedArrays(int A[], int m, int B[], int n)
{
int total = m + n;
// totla is even OR odd ?
if (total & 0x1) // even
return findKth(A, m, B, n, total / 2 + 1);
else // odd
return (findKth(A, m, B, n, total / 2)
+ findKth(A, m, B, n, total / 2 + 1)) / 2;
}
};
int main()
{
Solution s1;
int A[] = {6,7,8,9};
int B[] = {5,6};
cout << s1.findMedianSortedArrays(A, 4, B, 2) << endl;
return 0;
}
double findKth(int a[], int m, int b[], int n, int k)
{
//always assume that m is equal or smaller than n
if (m > n)
return findKth(b, n, a, m, k);
if (m == 0)
return b[k - 1];
if (k == 1)
return min(a[0], b[0]);
//divide k into two parts
int pa = min(k / 2, m), pb = k - pa;
if (a[pa - 1] < b[pb - 1])
return findKth(a + pa, m - pa, b, n, k - pa);
else if (a[pa - 1] > b[pb - 1])
return findKth(a, m, b + pb, n - pb, k - pb);
else
return a[pa - 1];
}
class Solution
{
public:
double findMedianSortedArrays(int A[], int m, int B[], int n)
{
int total = m + n;
if (total & 0x1)
return findKth(A, m, B, n, total / 2 + 1);
else
return (findKth(A, m, B, n, total / 2)
+ findKth(A, m, B, n, total / 2 + 1)) / 2;
}
};class Solution:
# @return a float
def getMedian(self, A, B, k):
# return kth smallest number of arrays A and B, assume len(A) <= len(B)
lenA = len(A); lenB = len(B)
if lenA > lenB: return self.getMedian(B, A, k)
if lenA == 0: return B[k-1]
if k == 1: return min(A[0], B[0])
pa = min(k/2, lenA); pb = k - pa
return self.getMedian(A[pa:], B, k - pa) if A[pa - 1] <= B[pb - 1] else self.getMedian(A, B[pb:], k - pb)
def findMedianSortedArrays(self, A, B):
lenA = len(A); lenB = len(B)
if (lenA + lenB) % 2 == 1:
return self.getMedian(A, B, (lenA + lenB) / 2 + 1)
else:
return 0.5 * ( self.getMedian(A, B, (lenA + lenB) / 2) + self.getMedian(A, B, (lenA + lenB) / 2 + 1) )LeetCode 第二题,Median of Two Sorted Arrays,布布扣,bubuko.com
LeetCode 第二题,Median of Two Sorted Arrays
标签:style blog http color 使用 os io for
原文地址:http://blog.csdn.net/suool/article/details/38343457