Median of Two Sorted Arrays
2014-04-14 14:31
281 查看
class Solution { public: double findMedianSortedArrays(int A[], int m, int B[], int n) { double lhs=0,rhs=0;//lhs为第(m+n)/2大的数 rhs为第(m+n)/2+1大的数 //O(m+n) int aIndex=0,bIndex=0,number=1,cur=0; while(aIndex < m || bIndex < n) { if(aIndex < m && bIndex < n) { if(A[aIndex] <= B[bIndex]) { cur=A[aIndex]; aIndex++; } else { cur=B[bIndex]; bIndex++; } } else if(aIndex < m && bIndex >=n) { cur=A[aIndex]; aIndex++; } else { cur=B[bIndex]; bIndex++; } if(number==(m+n)/2) { lhs=(double)cur; } if(number==(m+n)/2+1) { rhs=(double)cur; return ((m+n)%2)?rhs:(lhs+rhs)/2; } number++; } } };
相关文章推荐
- ruby 数组使用教程
- 探索PowerShell (八) 数组、哈希表(附:复制粘贴技巧)
- 学习使用PHP数组
- 一个PHP数组应该有多大的分析
- 将数组写入txt文件 var_export
- PHP操作数组的一些函数整理介绍
- PHP关联数组的10个操作技巧
- PHP去除数组中重复的元素并按键名排序函数
- PHP下对数组进行排序的函数
- PHP中常用数组处理方法实例分析
- PHP学习笔记之数组篇
- php 数组使用详解 推荐
- PHP extract 将数组拆分成多个变量的函数
- PHP中数组定义的几种方法
- javascript splice数组简单操作
- 在ASP编程中使用数组
- 数组与类使用PHP的可变变量名需要的注意的问题
- Javascript 数组添加一个 indexOf 方法的实现代码
- 用Javascript读取CheckBox数组的值的代码(兼容IE与firefox)
- 如何写JS数组sort的比较函数