• LeetCode 2.Median of Two Sorted Arrays


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

    Solution

     1 #include<iostream>
     2 #include<vector>
     3 using namespace std;
     4 class Solution {
     5 public:
     6 
     7     double findMedianSortedArrays(int A[], int m, int B[], int n) 
     8     {
     9         int  findNum=0;
    10         if((m+n)%2==0)
    11             findNum=2;
    12         else
    13             findNum=1;
    14 
    15         int leftOffsetA=0,leftOffsetB=0;
    16         int rightOffsetA=0,rightOffsetB=0;
    17         while(leftOffsetA+leftOffsetB+rightOffsetA+rightOffsetB!=m+n-findNum)
    18         {
    19             
    20             if(leftOffsetA==m|| rightOffsetA==m)
    21             {
    22                 //左右端都在B中
    23                 int halfLenthOfPartB=(n-1-leftOffsetB-rightOffsetB)/2;
    24                 leftOffsetB+=halfLenthOfPartB;
    25                 rightOffsetB+=halfLenthOfPartB;
    26                 break;
    27             }
    28             else if(leftOffsetB==n|| rightOffsetB==n)
    29             {
    30                 //左右端都在A中
    31                 int halfLenthOfPartA=(m-1-leftOffsetA-rightOffsetA)/2;
    32                 leftOffsetA+=halfLenthOfPartA;
    33                 rightOffsetA+=halfLenthOfPartA;
    34                 break;
    35             }
    36             else
    37             {
    38                 //左端还分别在A、B中
    39                 if(A[leftOffsetA]<B[leftOffsetB])
    40                     leftOffsetA++;
    41                 else
    42                     leftOffsetB++;
    43 
    44                 if(A[m-1-rightOffsetA]>B[n-1-rightOffsetB])
    45                     rightOffsetA++;
    46                 else
    47                     rightOffsetB++;
    48             }
    49         }
    50     //    cout<<"leftOffsetA:"<<leftOffsetA<<" rightOffsetA"<<rightOffsetA<<endl;
    51     //    cout<<"leftOffsetB:"<<leftOffsetB<<" rightOffsetB"<<rightOffsetB<<endl;
    52         
    53         //取出两段截断的int数据
    54         vector<int> ivec;
    55         for(int index_i=leftOffsetA;index_i<=m-1-rightOffsetA &&index_i<m;index_i++)
    56         {
    57             ivec.push_back(A[index_i]);
    58             //cout<<"A中取出:"<<A[index_i]<<endl;
    59         }
    60         for(int index_i=leftOffsetB;index_i<=n-1-rightOffsetB &&index_i<n;index_i++)
    61         {
    62             ivec.push_back(B[index_i]);
    63             //cout<<"B中取出:"<<B[index_i]<<endl;
    64         }
    65         if(findNum==1)
    66             return ivec[0];
    67         else
    68             return (ivec[0]+ivec[1])/2.0f;
    69         
    70     }
    71 };
  • 相关阅读:
    关于ArcGIS Server VS2010无法显示发布的地图服务
    NDK,动态链接库,JNI
    android开发中一些报错的解决方法
    查询死锁对象
    pb导入excel文件
    n_cst_ping在powerbuilder 11 中的变化,一面的程序可正常ping
    SQL拼音
    数据库优化
    android调用asp.net webservice,返回json结构
    iframe内部需要弹出浮层问题
  • 原文地址:https://www.cnblogs.com/kyokuhuang/p/4190249.html
Copyright © 2020-2023  润新知