Median of Two Sorted Arrays -- Leetcode
来源:互联网 发布:linux 复制整行 编辑:程序博客网 时间:2024/05/17 21:37
12.21 2014
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)).
class Solution {public: double findMedianSortedArrays(int A[], int m, int B[], int n) { vector<int> merge; int i,j,k; i=j=0; for(k=0;k<m+n;k++){ if(i<m && j<n){ if(A[i]<=B[j]){ merge.push_back(A[i]); i++; } else { merge.push_back(B[j]); j++; } } else if(i==m){ merge.push_back(B[j]); j++; } else if(j==n){ merge.push_back(A[i]); i++; } } if((m+n)%2==0) return (merge[(m+n)/2]+merge[(m+n)/2-1])/2.0; else return merge[(m+n)/2]; }};
总结:
1. 主要出错在了整数除法的地方
2. 主要先用merge sort变成新vector,使用下标访问找出中位数。
0 0
- LeetCode: Median of Two Sorted Arrays
- LeetCode Median of Two Sorted Arrays
- leetcode 26: Median of Two Sorted Arrays
- Leetcode 4 Median of Two Sorted Arrays
- [Leetcode] Median of Two Sorted Arrays
- LeetCode 4 - Median of Two Sorted Arrays
- [LeetCode]Median of Two Sorted Arrays
- leetcode Median of Two Sorted Arrays
- LeetCode-Median of Two Sorted Arrays
- [LeetCode] Median of Two Sorted Arrays
- [LeetCode] Median of Two Sorted Arrays
- [leetcode] Median of Two Sorted Arrays
- leetcode-004:Median of Two Sorted Arrays
- Leetcode 4 Median of Two Sorted Arrays
- leetcode之 median of two sorted arrays
- 【转载】【leetcode】Median of Two Sorted Arrays
- LeetCode - Median of Two Sorted Arrays
- [LeetCode]Median of Two Sorted Arrays
- 开源云计算技术系列(四)(Cloudera体验篇)
- 17周项目四,结构体计算某天是今年的多少天
- 前端优化《一》-----雅虎给出了优化网站加载速度的34条法则(包括Yslow规则22条
- Ubuntu各种文件解压命令总结
- 开源云计算技术系列三(10gen)安装配置
- Median of Two Sorted Arrays -- Leetcode
- linux resize命令参数及用法详解--linux设置终端窗口大小命令
- 第十七周项目3-胖子伤不起
- 17周项目4--日期结构体
- UVA - 10194 Football (aka Soccer) 多种排序
- 帧布局
- 第十七周项目二:引用做形参:引用类型
- 第十七周项目二引用作形参(2)引用类型作形参
- 高内聚低耦合