# [LeetCode]Median of Two Sorted Arrays

c++实现时，两句话就可以了，两个vector放一起
sort一些即可

//交换两个元素
void swap(int *a, int *b){
int tmp = *a;
*a = *b;
*b = tmp;
}

int median3(int *v, int left, int right){
int median = (left+right)/2;
//先将最小的放在left
if(v[median]<v[left])
swap(v+median, v+left);
if(v[right]<v[left])
swap(v+right,v+left);
//中间的数放在median
if(v[median]>v[right])
swap(v+median, v+right);
swap(v+median, v+right-1);
return v[right-1];
}
void InsertSort(int *v, int N){
int tmp;int i,j;
for(i = 1; i != N; ++i){
tmp = v[i];
for(j = i; j!=0&&v[j-1]>tmp; --j)
v[j]=v[j-1];
v[j] =tmp;
}
}
void QuickSort(int *v, int left, int right){
if((right-left)>16){
int i = left, j = right-1;
int pivot = median3(v, left, right);
for(;;){
while(v[++i]<pivot){}
while(v[--j]>pivot){}
if(i<j)
swap(v+i,v+j);
else
break;
}
swap(v+right-1, v+i);
QuickSort(v,left,i-1);
QuickSort(v,i+1,right);
}
else
InsertSort(v+left, right-left+1);

}
void Quick_Sort(int *v, int N){
QuickSort(v,0,N-1);
}
double findMedianSortedArrays(int* nums1, int nums1Size, int* nums2, int nums2Size) {
int *v = (int*)malloc(sizeof(int)*(nums1Size+nums2Size));
for(int i = 0; i!= nums1Size; ++i)
v[i] = nums1[i];
for(int j = 0; j!= nums2Size; ++j)
v[nums1Size+j]=nums2[j];
int v_size = nums1Size+nums2Size;
Quick_Sort(v,v_size);
if(v_size%2)
return v[(v_size-1)/2];
else
return (v[v_size/2]+v[v_size/2-1])/2.0;
}

o(m+n）寻找kth

#include<iostream>

using namespace std;

int findkth(int *a,int m, int *b,int n,int k){
if(m==0)
return b[k-1];
if(n==0)
return a[k-1];
if(k==1)
return a[0]<b[0]?a[0]:b[0];
int idx = 1;    //现在指向第k大的数
int i = 0;
int j = 0;
while(idx!=k){
while(a[i]<b[j]){if(idx==k) return a[i];++i;++idx;}
while(a[i]>=b[j]){if(idx==k) return b[j];++j; ++idx;}
}
}

int main(){
int a[4]={1,2,3,6};
int b[2]={5,7};
cout <<findkth(a,4,b,2,5)<<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;
}
};


• 本文已收录于以下专栏：

举报原因： 您举报文章：[LeetCode]Median of Two Sorted Arrays 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)