首页 > 其他 > 详细

Leetcode 4 Median of Two Sorted Arrays

时间:2015-03-27 16:57:05      阅读:130      评论:0      收藏:0      [点我收藏+]

1.题目要求

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

2.分析

这道题,首先需要理解中位数(Median)的定义。

举例:1,2,3的中位数是2,1,2,3,4的中位数是(2+3)/2=2.5。

弄清楚定义后,可以想出O(m+n)时间复杂度的算法,也就是两个下班分别指向A和B比较当前数A[i]和B[j],较小者的下标做自增操作,指导进行(m+n)/2次自增操作,或者某一个数组下标越界为止,代码如下:

 1 class Solution {
 2 public:
 3     double findMedianSortedArrays(int A[], int m, int B[], int n) {
 4         int middle ;
 5         int temp = (m+n)%2;
 6         if(temp == 1)
 7             middle = (m+n)/2;
 8         else
 9             middle=(m+n)/2-1;
10         int i=0,j=0;
11         int count=0;
12         int flag=0;
13         double res;
14         while (count<middle&&i<m&&j<n)
15         {
16             if(A[i]<B[j]){
17                 i++;
18             }
19             else if (A[i]>B[j])
20             {
21                 j++;
22             }else
23             //当A[i]==B[j]时,两个数组下标移动需要取决于下一个数,比如{1,1}和{1,2};
24             //1==1因此需要i++,如果是{1,2}和{1,1},则j++
25             {
26                 if(i+1<m&&A[i+1]==A[i])
27                     i++;
28                 else if(j+1<n&&B[j+1]==B[j])
29                     j++;
30                 else
31                     i++;
32 
33 
34             }
35             count++;
36         }
37         
38         if (i==m)//数组A已经越界
39         {
40             if(temp==0){
41                 res = (B[j+middle-count]+B[j+middle-count+1])/2.0;
42             }else
43                 res = B[j+middle-count];
44 
45         }else if (j==n)//数组B已经越界
46         {
47             if(temp==0){
48                 res = (A[i+middle-count]+A[i+middle-count+1])/2.0;
49             }else
50                 res = A[i+middle-count];
51         }else
52         {
53             if (temp == 0)
54             {
55                 if(i+1<m && A[i+1]<B[j])
56                     res = (A[i]+A[i+1])/2.0;
57                 else if(j+1<n && B[j+1]<A[i])
58                     res = (B[j]+B[j+1])/2.0;
59                 else
60                     res = (B[j]+A[i])/2.0;
61 
62             }else
63             {
64                 res = A[i]>B[j] ? B[j]:A[i];
65             }
66         }
67 
68         return  res;
69 
70     }
71 };

提交代码,呵呵,能够Accepted。但是,题目要求时O(log(m+n)),因此我们需要想其他更好的算法,重点就是抓住sorted这个字眼。看到时间复杂度O(log(m+n)),我们会立刻想到二分查找思想,因此我们就按这个思路去寻找更好的算法。

 

Leetcode 4 Median of Two Sorted Arrays

原文:http://www.cnblogs.com/LCCRNblog/p/4371888.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!