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

 比较容易想到的做法是O(n),merge两个数组,然后求中值。

 1 public class Solution {
 2     public double findMedianSortedArrays(int A[], int B[]) {
 3         // Start typing your Java solution below
 4         // DO NOT write main() function
 5         double result = 0;
 6         int lengthA = A.length;
 7         int lengthB = B.length;
 8         
 9         int[] combinedArray = new int[lengthA + lengthB];
10         for(int i = 0; i < lengthA; i++){
11             combinedArray[i] = A[i];
12         }
13         for(int j = 0; j < lengthB; j++){
14             combinedArray[lengthA + j] = B[j];
15         }
16         
17         Arrays.sort(combinedArray);
18         
19         if(((lengthA + lengthB) % 2) !=0){
20             result = combinedArray[(lengthA + lengthB) / 2];
21            
22         } else {
23             result = (combinedArray[(lengthA + lengthB) / 2] + combinedArray[(lengthA + lengthB) / 2 - 1]) / 2.0;
24             
25         }
26         return result;
27     }
28 }

 O(log(m+n))第一映像是使用二分搜索

 

 1 public class Solution {
 2     public double findMedianSortedArrays(int A[], int B[]) {
 3         // Start typing your Java solution below
 4         // DO NOT write main() function
 5         int aLen = A.length;
 6         int bLen = B.length;
 7         if((aLen + bLen) % 2 ==0){
 8             return (getKthElement(A, 0, aLen - 1, B, 0, bLen - 1, (aLen + bLen) / 2) + getKthElement(A, 0, aLen - 1, B, 0, bLen - 1, (aLen + bLen) / 2 + 1)) / 2.0;
 9         } else {
10             return getKthElement(A, 0, aLen - 1, B, 0, bLen - 1, (aLen + bLen) / 2 + 1);
11         }
12     }
13     
14     public int getKthElement(int A[], int aBeg, int aEnd, int B[], int bBeg, int bEnd, int k){
15         if(aBeg > aEnd){
16             return B[bBeg + (k - 1)];
17         }
18         if(bBeg > bEnd){
19             return A[aBeg + (k - 1)];
20         }
21         
22         int aMid = (aBeg + aEnd) >> 1;
23         int bMid = (bBeg + bEnd) >> 1;
24         int len = aMid - aBeg + bMid - bBeg + 2;
25         
26         if(len > k){
27             if(A[aMid] < B[bMid]){
28                 return getKthElement(A, aBeg, aEnd, B, bBeg, bMid - 1, k);
29             } else {
30                 return getKthElement(A, aBeg, aMid - 1, B, bBeg, bEnd, k);
31             }
32         } else {
33             if(A[aMid] < B[bMid]){
34                 return getKthElement(A, aMid + 1, aEnd, B, bBeg, bEnd, k - (aMid - aBeg + 1));
35             } else {
36                 return getKthElement(A, aBeg, aEnd, B, bMid + 1, bEnd, k - (bMid - bBeg + 1));
37             }
38         }
39     } 
40 }

(a + b) >> 1 + 1

>> 的优先级比 + 要低,上述等价于(a + b) >> (1 + 1)

 

ref http://www.cnblogs.com/longdouhzt/archive/2013/03/04/2943572.html

posted @ 2013-06-15 18:07  feiling  阅读(460)  评论(0编辑  收藏  举报