Median of Two Sorted Arrays
来源:互联网 发布:下载qq软件2016 编辑:程序博客网 时间:2024/05/16 17:16
There are two sorted arrays nums1 and nums2 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)).
Example 1:
nums1 = [1, 3]nums2 = [2]The median is 2.0
Example 2:
nums1 = [1, 2]nums2 = [3, 4]The median is (2 + 3)/2 = 2.5
class Solution {public: double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) { if (0 == (nums1.size() + nums2.size())) { return 0.0; } vector<int> tmp; tmp.reserve(nums1.size() + nums2.size()); int i = 0, j = 0; for (; ((i < nums1.size()) && (j < nums2.size())); ) { if (nums1[i] < nums2[j]) { tmp.push_back(nums1[i++]); } else { tmp.push_back(nums2[j++]); } } if (i < nums1.size()) { for (int l = i; l < nums1.size(); l++) { tmp.push_back(nums1[l]); } } else { for (int l = j; l < nums2.size(); l++) { tmp.push_back(nums2[l]); } } if (1 == tmp.size()) { return (double)tmp[0]; } else if (0 == (tmp.size()%2)) { return ((double)tmp[tmp.size()/2] + (double)tmp[tmp.size()/2 - 1])/2.0; } else { return (double)tmp[tmp.size()/2]; } }};
阅读全文
0 0
- Median of Two Sorted Arrays
- Median of Two Sorted Arrays
- Median of Two Sorted Arrays
- Median of two sorted arrays
- Median of Two Sorted Arrays
- Median of Two Sorted Arrays
- Median of Two Sorted Arrays
- Median of Two Sorted Arrays
- Median of Two Sorted Arrays
- Median of Two Sorted Arrays
- median-of-two-sorted-arrays
- Median of Two Sorted Arrays
- Median of Two Sorted Arrays
- Median of Two Sorted Arrays
- Median of Two Sorted Arrays
- Median of Two Sorted Arrays
- Median of Two Sorted Arrays
- Median of Two Sorted Arrays
- 组合数学-- 放回取样基础公式证明
- List调用remove(object);方法无法删除!
- mysql5.7在centos上安装的完整教程以及相关的“坑”
- The most simple and easy to use Qt GUI library plug-in FTStyle (1)
- 代码重构(三):数据重构规则
- Median of Two Sorted Arrays
- MYSQL中limit以及oder by混用产生的一些错误
- Python中常用的字符串内建函数
- Erlang到底好在哪里 -- 聊聊我使用Erlang的经历与感受
- java的Exchanger 两个线程可以交换对象Demo
- GoEasy实现后端向前端页面的推送
- iOS开发之xcode统计项目代码行数
- 神经网络入门(neural network)---初级应用篇
- 使用mysql企业版备份软件来设置一个从库:Setting Up a New Replication Slave