【LeetCode OJ】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)).
解题思路:将两个有序数组合并为一个,设置一个 Map<Integer, Integer>,key值为序号,value值为数值,m+n个数的中位数要分两种情况讨论:
1、m+n为奇数:则(m+n)/2即为其中位数
2、m+n为偶数:则(((m+n)/2-1)+(m+n)/2)/2为中位数
示例代码如下:
public class Solution
{
public static double findMedianSortedArrays(int[] nums1, int[] nums2)
{
int m=nums1.length;
int n=nums2.length;
int index; //中位数的位置
//m+n为奇数
double result;
if(m==1&&n==0)
return nums1[0];
if(m==0&&n==1)
return nums2[0];
Map<Integer, Integer> map=new TreeMap<Integer, Integer>();
int i=0,j=0,k=0;
while(i<m&&j<n)
{
if(nums1[i]<=nums2[j])
{
map.put(k,nums1[i]);
i++;
k++;
}
else
{
map.put(k,nums2[j]);
j++;
k++;
}
}
while(i<m)
{
map.put(k,nums1[i]);
i++;
k++;
}
while(j<n)
{
map.put(k,nums2[j]);
k++;
j++;
}
//m+n为奇数
if((m+n)%2==1)
{
result=map.get((m+n)/2);
return result;
}
else
{
result=(double)(map.get((m+n)/2-1)+map.get((m+n)/2))/2;
return result;
}
} }
【LeetCode OJ】Median of Two Sorted Arrays的更多相关文章
- LeetCode OJ 4. 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 ...
- 【LeetCode OJ】Remove Duplicates from Sorted Array
题目:Given a sorted array, remove the duplicates in place such that each element appear only once and ...
- 《LeetBook》leetcode题解(4): Median of Two Sorted Arrays[H]——两个有序数组中值问题
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- leetcode.C.4. Median of Two Sorted Arrays
4. Median of Two Sorted Arrays 这应该是最简单最慢的方法了,因为本身为有序,所以比较后排序再得到中位数. double findMedianSortedArrays(in ...
- 【leetcode】Median of Two Sorted Arrays
题目简述: There are two sorted arrays A and B of size m and n respectively. Find the median of the two s ...
- 【leetcode】Median of Two Sorted Arrays(hard)★!!
There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted ...
- 【LeetCode每天一题】Median of Two Sorted Arrays(两数组中的中位数)
There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the tw ...
- 【leetcode刷题笔记】Median of Two Sorted Arrays
There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted ...
- 【leedcode】 Median of Two Sorted Arrays
https://leetcode.com/problems/median-of-two-sorted-arrays/ There are two sorted arrays nums1 and num ...
随机推荐
- 第三百五十四节,Python分布式爬虫打造搜索引擎Scrapy精讲—数据收集(Stats Collection)
第三百五十四节,Python分布式爬虫打造搜索引擎Scrapy精讲—数据收集(Stats Collection) Scrapy提供了方便的收集数据的机制.数据以key/value方式存储,值大多是计数 ...
- 关于 initWithNibName 和 loadNibNamed 的区别和联系
转载自:http://jianyu996.blog.163.com/blog/static/1121145552012102293653906/ 关于 initWithNibName 和 loadNi ...
- at 命令
每天什么时候执形at 12:00 /every:Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday shutdown -r
- asp.net 局域网中获取 client的机器名
//获取客户端计算机名称 System.Net.IPAddress clientIP = System.Net.IPAddress.Parse(Request.UserHostAddress);//根 ...
- spring cloud feign 上传文件报not a type supported by this encoder解决方案
上传文件调用外部服务报错: not a type supported by this encoder 查看SpringFormEncoder类的源码: public class SpringFormE ...
- ubuntu安装mongo数据库
安装mongo数据库,在shell下输入 sudo apt-get install mongodb 如果需要在Python中使用mongo数据库,还需要额外安装Python封装库 pip instal ...
- 如何能延长windows server 2008 R2激活期 .
当windows server 2008 R2使用已经到期的时候,要求激活,我们可以通过以下命令,延长激活期. 在运行中输入:slmgr.vbs -rearm 重新启动windows server 2 ...
- 本地文件到通过flume到hdfs
配置文件 cd /usr/app/flume1.6/conf vi flume-dirTohdfs.properties #agent1 name agent1.sources=source1 age ...
- datatable删除一行方法
t.row($(e).parents('tr')[0]).remove().draw(false); t为定义的datatable对象,row里面传入当前行的DOM元素.
- 手机端网页使用html5地理定位获取位置失败的解决办法
网上有很多关于html5 geolocation 获取地理定位的方法,我试了下,只有在IE edge浏览器可以成功获取到,在chrome,firefox,手机端的safari,QQ浏览器,微信浏览器, ...