• 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

  • 相关阅读:
    使用递归,计算斐波那契数列
    Javascript模块化编程 require.js使用详解
    逻辑很重要:一句sql语句的事,自己却想了半天,绕了个大弯子
    select options常用操作
    select 下拉菜单Option对象使用add(elements,index)方法动态添加
    $().change事件
    jQuery验证控件jquery.validate.js使用说明
    copy(source,destination)拷贝文件
    Linux常用命令
    纯js实现分页
  • 原文地址:https://www.cnblogs.com/feiling/p/3137843.html
Copyright © 2020-2023  润新知