leetcode 4. Median of Two Sorted Arrays
https://leetcode.com/problems/median-of-two-sorted-arrays/
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) {
vector<int> nums3;
int i=,j=,k=;
int m =(nums1.size()+nums2.size());
while(i<nums1.size()&&j<nums2.size()) {
if(nums1[i]<nums2[j])
nums3.push_back(nums1[i++]);
else
nums3.push_back(nums2[j++]);
k++;
if(k>m/)
{
if(m%)
return nums3[k-];
else
return (nums3[k-]+nums3[k-])/2.0;
}
}
while(i<nums1.size()){
nums3.push_back(nums1[i++]);
k++;
if(k>m/)
{
if(m%)
return nums3[k-];
else
return (nums3[k-]+nums3[k-])/2.0;
}
}
while(j<nums2.size()){
nums3.push_back(nums2[j++]);
k++;
if(k>m/)
{
if(m%)
return nums3[k-];
else
return (nums3[k-]+nums3[k-])/2.0;
}
}
return ;
}
};
在leetcode中时间为48ms。
leetcode 4. Median of Two Sorted Arrays的更多相关文章
- 【算法之美】求解两个有序数组的中位数 — leetcode 4. Median of Two Sorted Arrays
一道非常经典的题目,Median of Two Sorted Arrays.(PS:leetcode 我已经做了 190 道,欢迎围观全部题解 https://github.com/hanzichi/ ...
- LeetCode(3) || Median of Two Sorted Arrays
LeetCode(3) || Median of Two Sorted Arrays 题记 之前做了3题,感觉难度一般,没想到突然来了这道比较难的,星期六花了一天的时间才做完,可见以前基础太差了. 题 ...
- LeetCode 4 Median of Two Sorted Arrays (两个数组的mid值)
题目来源:https://leetcode.com/problems/median-of-two-sorted-arrays/ There are two sorted arrays nums1 an ...
- Leetcode 4. Median of Two Sorted Arrays(二分)
4. Median of Two Sorted Arrays 题目链接:https://leetcode.com/problems/median-of-two-sorted-arrays/ Descr ...
- LeetCode 4. Median of Two Sorted Arrays & 归并排序
Median of Two Sorted Arrays 搜索时间复杂度的时候,看到归并排序比较适合这个题目.中位数直接取即可,所以重点是排序. 再来看看治阶段,我们需要将两个已经有序的子序列合并成一个 ...
- 第三周 Leetcode 4. Median of Two Sorted Arrays (HARD)
4. Median of Two Sorted Arrays 给定两个有序的整数序列.求中位数,要求复杂度为对数级别. 通常的思路,我们二分搜索中位数,对某个序列里的某个数 我们可以在对数时间内通过二 ...
- Leetcode 4. Median of Two Sorted Arrays(中位数+二分答案+递归)
4. Median of Two Sorted Arrays Hard There are two sorted arrays nums1 and nums2 of size m and n resp ...
- LeetCode 004 Median of Two Sorted Arrays
题目描述:Median of Two Sorted Arrays There are two sorted arrays A and B of size m and n respectively. F ...
- leetcode之 median of two sorted arrays
这是我做的第二个leetcode题目,一开始以为和第一个一样很简单,但是做的过程中才发现这个题目非常难,给人一种“刚上战场就踩上地雷挂掉了”的感觉.后来搜了一下leetcode的难度分布表(leetc ...
随机推荐
- 5. Longest Palindromic Substring
Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...
- Linux流量监控工具
http://www.vpser.net/manage/iftop.html 在类Unix系统中可以使用top查看系统资源.进程.内存占用等信息.查看网络状态可以使用netstat.nmap等工具.若 ...
- wkhtmltopdf乱码解决方案
在CentOS下使用wkhtmltopdf将html页面转换成pdf的时候对于某些页面可能会出现转换成的pdf中很多字符乱码的情况,更怪异的是直接在命令行下运行一切正常,但在httpd+php下使用却 ...
- 13 年的 Bug 调试经验总结
在<Learning From Your Bugs>一文中,我写了关于我是如何追踪我所遇到的一些最有趣的bug.最近,我回顾了我所有的194个条目(从13岁开始),看看有什么经验教训是我可 ...
- selenium之 定位以及切换frame(iframe)
Set<String> windows = driver.getWindowHandles(); int count = 0; for(String handl ...
- <2016-1-29>
1.打电话,发短信,发邮件 //打电话 <script> function call(){ var call = new CallManage(); call.call('10086'); ...
- linux php bom
之前都是用cleanbom.php来去除BOM的.今天运行提示没有异常文件,但是用Fiddler还是看到了EF BB BF,删除缓存也无效,将cleanbom.php上传到生产环境也提示没有异常. 这 ...
- SqlCommand执行带GO的SQL脚本文件
今天工作中遇到了这个问题,其实只要把GO替换成“;”就行了,其它人写的例子用Split来拆分这一个脚本文件的内容,完全没有必要.希望对你有用.
- Ejabberd作为推送服务的优化手段
AVOS Cloud目前还在用Ejabberd做Android的消息推送服务.当时选择Ejabberd,是因为Ejabberd是一个发展很长时间的XMPP实现,并且基于Erlang,设想能在我们自主研 ...
- 网络编程(一):用C#下载网络文件的2种方法
使用C#下载一个Internet上的文件主要是依靠HttpWebRequest/HttpWebResonse和WebClient.具体处理起来还有同步和异步两种方式,所以我们其实有四种组合. 1.使用 ...