double findMedianSortedArrays(int A[], int m, int B[], int n) {
    if((m+n)%2) {
        return findKth(A,m,B,n,(m+n)/2+1);
    } else {
        return 0.5*(findKth(A,m,B,n,(m+n)/2+1) + findKth(A,m,B,n,(m+n)/2));
    }
}
 double findKth(vector<int>& nums1, vector<int>& nums2, int start1, int len1, int start2, int len2, int k) {
     if (len1 > len2) {
         return findKth(nums2, nums1, start2, len2, start1, len1, k);
     }
     
     if (len1 == 0) {
         return nums2[start2 + k - 1];
     }
     
     if (k == 1) {
         return min(nums1[start1], nums2[start2]);
     }
     
     int p1 = min(k / 2, len1);
     int p2 = k - p1;
     if (nums1[start1 + p1 - 1] > nums2[start2 + p2 - 1]) {
         return findKth(nums1, nums2, start1, len1, start2 + p2, len2 - p2, k - p2);
     }
     else if (nums1[start1 + p1 - 1] < nums2[start2 + p2 - 1]) {
         return findKth(nums1, nums2, start1 + p1, len1 - p1, start2, p2, k - p1);
     }
     else {
         return nums1[start1 + p1 - 1];
     }
 }
int findKth(int* l, int ls, int* r, int rs, int k) {

	if (ls > rs) {
		return findKth(r, rs, l, ls, k);
	}

	if (ls == 0) {
		return r[k - 1];
	}

	if (k == 1) {
		return l[0] < r[0] ? l[0] : r[0];
	}

	int lk = k / 2 < ls ? k / 2 : ls;
	int rk = k - lk;

	if (l[lk - 1] < r[rk - 1]) {
		return findKth(l + lk, ls - lk, r, rs, k - lk);
	}
	else if (l[lk - 1] > r[rk - 1]) {
		return findKth(l, ls, r + rk, rs - rk, k - rk);
	}
	else {
		return l[lk - 1];
	}
	
}
 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;
 }
 double findMedianSortedArrays(int A[], int m, int B[], int n) {
     if ((n + m) % 2 == 0) {
         return .5 * findKth(A, m, B, n, (n + m) / 2 - 1) +
         .5 * findKth(A, m, B, n, (n + m) / 2);
     }
     return findKth(A, m, B, n, (n + m) / 2);
 }
 double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
     int m = nums1.size(), n = nums2.size();
     if ((m + n) & 0x1) //odd
         return findKth(nums1, nums2, 0, 0, (m + n)/2 + 1);
     else
         return (findKth(nums1, nums2, 0, 0, (m + n)/2) + findKth(nums1, nums2, 0, 0, (m + n)/2 + 1)) /2;
 }
 double findMedianSortedArrays(int A[], int m, int B[], int n) {
     // Note: The Solution object is instantiated only once and is reused by each test case.
     
     if((m+n) % 2 == 0)
        return ( findKth(A, m, B, n, (m+n+1)/2) + findKth(A, m, B, n, (m+n+2)/2) )/2.0;
     else
        return findKth(A, m, B, n, (m+n+1)/2);
 }
double findMedianSortedArrays(int* l, int ls, int* r, int rs) {
    
    if ((ls + rs) &0x01) {
        return findKth(l, ls, r, rs, (ls+rs)/2+1);
    } else {
        return (double)(findKth(l, ls, r, rs, (ls+rs)/2) + findKth(l, ls, r, rs, (ls+rs)/2+1))/2;
    }
    
}
  double medianOfSortedArrays(vector<int> &A, vector<int> &B) {
    int m = A.size(), n = B.size();

    if((m+n)%2 == 0) {
      return (findKth(A, B, (m+n+1)/2, 0, 0)
            + findKth(A, B, (m+n+2)/2, 0, 0)) / 2.0;
    } else
      return findKth(A, B, (m+n+1)/2, 0, 0);
  }
 double findMedianSortedArrays(int A[], int m, int B[], int n) {
   int all = m + n;
   if (all % 2 == 1){
     return findKth(A,m,B,n,all / 2 + 1);
   }
   else {
     return (findKth(A,m,B,n,all / 2) + findKth(A,m,B,n,all / 2 + 1)) / 2.0;
   }
 }
double findMedianSortedArrays(int* nums1, int nums1Size, int* nums2, int nums2Size)
{
	int total = nums1Size + nums2Size;
	if (total & 0x1)
		return findKth(nums1, nums1Size, nums2, nums2Size, total/2 + 1);
	else 
		return (findKth(nums1, nums1Size, nums2, nums2Size, total/2) + 
				findKth(nums1, nums1Size, nums2, nums2Size, total/2 + 1)) / 2; 
}
    double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
	int m = nums1.size(), n = nums2.size();
	if((m+n) % 2 == 1) {
            return (double)findKth(nums1, 0, nums2, 0, (m+n)/2 + 1);
        } else {
            int a = findKth(nums1, 0, nums2, 0, (m+n)/2);
            int b = findKth(nums1, 0, nums2, 0, (m+n)/2 + 1);
            return (double)(a+b) / 2.0;
        }
    }
 // The overall run time complexity should be O(log (m+n)).
 double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
     int len = nums1.size() + nums2.size();
     
     if (len % 2 == 0) {
         return (findKth(nums1, nums2, 0, nums1.size(), 0, nums2.size(), len / 2) + findKth(nums1, nums2, 0, nums1.size(), 0, nums2.size(), len / 2 + 1)) / 2.0f;
     }
     else {
         return findKth(nums1, nums2, 0, nums1.size(), 0, nums2.size(), len / 2 + 1);
     }
 }
	double findMedianSortedArrays(int A[], int m, int B[], int n)
	{
        int total = m + n;
        if (total % 2 == 1) {
            return findKth(A, m, B, n, total/2);
        } else {
            return (findKth(A, m, B, n, total/2-1) + 
                    findKth(A, m, B, n, total/2))/2;
        }
	}
 double findMedianSortedArrays(int A[], int m, int B[], int n) {
     int len=m+n;
     if(len==0) return 0.0;
     
     if(len & 1) {
         return findKth(A, m, B, n, len/2+1);
     } else {
         return (findKth(A, m, B, n, len/2)+findKth(A, m, B, n, len/2+1))/2.0;
     }
 }
 double findKth(int A[], int m, int B[], int n, int k) {
     if(m<n) return findKth(B, n, A, m, k);
     if(n==0) return A[k-1];
     if(k<2) return min(A[0],B[0]);
     int ib=min(n, k/2), ia=k-ib;
     if(A[ia-1]==B[ib-1]) return A[ia-1];
     else if(A[ia-1]<B[ib-1]) {
         return findKth(A+ia, m-ia, B, n, k-ia);
     } else return findKth(A, m, B+ib, n-ib, k-ib);
 }
double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
	int m = nums1.size();
	int n = nums2.size();
	int total = m + n;
	if (total & 0x1)
	  return findKth(nums1, m, nums2, n, total/2+1, 0, 0);
	else
	  return (findKth(nums1, m, nums2, n, total/2, 0, 0) + findKth(nums1, m, nums2, n, total/2+1, 0, 0))/2.0;

}
/* 
 * 奇数时,返回[len/2]
 * 偶数时,返回[len/2 - 1] 和 [len / 2]的平均值 */
double findMedianSortedArrays(int *A , int m, int *B, int n) 
{
	int total = m + n;
	//总个数是奇数,则返回第total/2 - 1小的数。例如,一共5个数,则返回从小到大第3个数                                      
	if (total & 0x1)
		return findKth(A, m, B, n, total / 2 + 1);
	//总个数是偶数,则返回第total/2小和total/2 + 1小的数。例如,一共4个数,则返回从小到大第2和第3个数的平均值              
	else
		return (findKth(A, m, B, n, total / 2)
				+ findKth(A, m, B, n, total / 2 + 1)) / 2;  	
}
Exemple #19
0
//http://blog.csdn.net/u010738052/article/details/50450153
double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
    int m = nums1.size(), n = nums2.size();
    int k = (m + n) / 2;
    int num1 = findKth(nums1, 0, m, nums2, 0, n, k + 1);
    if ((n + m) % 2 == 0)
    {
        int num2 = findKth(nums1, 0, m, nums2, 0, n, k);
        return (num1 + num2) / 2.0;
    }
    else return num1;
}
 double findMedianSortedArrays(int A[], int m, int B[], int n) {
     /* https://oj.leetcode.com/problems/median-of-two-sorted-arrays/
     Find the kth smallest element of two sorted array.
     */
     
     assert(m + n > 0);
     if ((m + n) % 2 == 1) {
         return (double)findKth(A, m, B, n, (m+n+1)/2);
     }
     return ((double)findKth(A, m, B, n, (m+n)/2) + (double)findKth(A, m, B, n, 1+(m+n)/2)) / 2;
 }
 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 min(A[0], B[0]);
     
     int i = min(k/2, m), j = min(k/2, n);
     if (A[i-1] < B[j-1]) {
         return findKth(A+i, m-i, B, n, k-i);
     }
     return findKth(A, m, B+j, n-j, k-j);
 }
 double findMedianSortedArrays(int A[], int m, int B[], int n) {
     // Start typing your C/C++ solution below
     // DO NOT write int main() function
     int k = m + n;
     if (k & 0x1)
     {
         return findKth(A, m, B, n, k / 2 + 1);
     } else
     {
         return (findKth(A, m, B, n, k / 2) + findKth(A, m, B, n, k / 2 + 1)) / 2;
     }
 }
 double findKth(int A[], int m, int B[], int n, int k) {
     // find k_th element in A and B
     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]);
     
     // m <= n
     int pa = min(k/2, m);
     int pb = k - pa;
     
     return (A[pa-1] <= B[pb-1]) ? findKth(A+pa, m-pa, B, n, pb) : findKth(A, m, B+pb, n-pb, pa);
 }
 double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
     int sum = nums1.size() + nums2.size();
     double ret;
     
     if (sum & 1) {
         ret = findKth(nums1, nums2, 0, 0, sum / 2 + 1);
     } else {
         ret = ((findKth(nums1, nums2, 0, 0, sum / 2)) +
                 findKth(nums1, nums2, 0, 0, sum / 2 + 1)) / 2.0;
     }
     return ret;
 }
Exemple #25
0
//method 1: Inorder traversal, O(n) where n is the total nodes in tree.
// part 1: recursive version
void findKth(node* root, int& k){
	if (!root || k < 0)
		return;
	
	findKth(p->left, k);
	--k;
	if (k == 0){
		printf("%d", p->val);
		return;
	}
	
	findKth(p->right, k);
}
Exemple #26
0
 double findMedianSortedArrays(int Nums1[],int m,int Nums2[],int n) 
 {
     int midNum=(m+n);
     
     if(midNum%2==0)
     {
         return (findKth(Nums1,m,Nums2,n,midNum/2)+findKth(Nums1,m,Nums2,n,midNum/2+1))/2;
     }
     else
     {
         return findKth(Nums1,m,Nums2,n,midNum/2);
     }
 }
double findKth(int A[], int m, int B[], int n, int k){
    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]);
    
    int pa = min(m, k/2), pb = k - pa;
    if (A[pa - 1] == B[pb - 1])
        return A[pa - 1];
    else if (A[pa - 1] < B[pb - 1])
        return findKth(A + pa, m - pa, B, n, k - pa);
    else
        return findKth(A, m, B + pb, n - pb, k - pb);
}
double findMedianSortedArrays(int* nums1, int nums1Size, int* nums2, int nums2Size) {
    int len;

    len1 = nums1Size;
    len2 = nums2Size;
    len = len1 + len2;

    if(len % 2)
        return findKth(nums1, 0, nums2, 0, len / 2 + 1);
    else
        return (findKth(nums1, 0, nums2, 0, len / 2) +
            findKth(nums1, 0, nums2, 0, len / 2 + 1)) / 2.0;
}
 double findKth(int A[], int m, int B[], int n, int target){
   if (m < n){
     return findKth(B,n,A,m,target);
   }
   if (n == 1){
     if (target == m + 1){
       return std::max(A[m-1],B[0]);
     }
     else if (target == 1){
       return std::min(A[0],B[0]);
     }
     else{
       return std::max(std::min(A[target-1],B[0]),A[target-2]);
     }
   }
   else if (n == 0){
     return A[target-1];
   }
   else{
     int indexA,indexB;
     if (target / 2 > n){
       indexB = n - 1;
       indexA = target - n + 1;
     }
     else{
       if (target % 2 == 0){
         indexA = target / 2;
         indexB = indexA;
       }
       else{
         indexB = target / 2;
         indexA = indexB + 1;
       }
     }
     if (A[indexA-1] > B[indexB-1]){
       B = B + indexB;
       return findKth(A,indexA,B,n-indexB,target-indexB);
     }
     else if (A[indexA-1] == B[indexB-1]){
       return A[indexA-1];
     }
     else{
       A = A + indexA;
       return findKth(A,m-indexA,B,indexB,target-indexA);
     }
     
   }
 }
 double findKth(int A[], int m, int B[], int n, int k) {
     if (m > n) return findKth(B, n, A, m, k);
     
     //m <= n, k = (m+n+1)/2, so m <= k <= (n+1)
     if (m == 0) return B[k-1];
     if (k == 1) return min(A[0], B[0]);
     
     int i = min(k/2, m);
     int j = k - i;
     int a = A[i-1];
     int b = B[j-1];
     
     if (a < b) return findKth(A+i, m-i, B, n, k-i);
     else if (a > b) return findKth(A, m, B+j, n-j, k-j);
     else return a;
 }