题目链接: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的更多相关文章

  1. 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 ...

  2. 【LeetCode OJ】Remove Duplicates from Sorted Array

    题目:Given a sorted array, remove the duplicates in place such that each element appear only once and ...

  3. 《LeetBook》leetcode题解(4): Median of Two Sorted Arrays[H]——两个有序数组中值问题

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

  4. leetcode.C.4. Median of Two Sorted Arrays

    4. Median of Two Sorted Arrays 这应该是最简单最慢的方法了,因为本身为有序,所以比较后排序再得到中位数. double findMedianSortedArrays(in ...

  5. 【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 ...

  6. 【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 ...

  7. 【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 ...

  8. 【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 ...

  9. 【leedcode】 Median of Two Sorted Arrays

    https://leetcode.com/problems/median-of-two-sorted-arrays/ There are two sorted arrays nums1 and num ...

随机推荐

  1. myeclipse重新添加spring支持

    需求:添加一次可能失败,需要再添加,但是一般点击右键add spring capabilities 不存在了 解决办法: 打开工程找到.project 注释掉spring支持 重新项目右键加入支持即可 ...

  2. Android seLinux 设置

    在android O上添加服务.在访问一些路径的时出现了权限的问题,将seLinux关闭之后运行成功.所以需要设置相关的权限. 参考文档: http://blog.csdn.net/tung214/a ...

  3. e816. 创建工具栏

    A toolbar can be either horizontal or vertical. When the orientation is horizontal, the objects in t ...

  4. C# IEnumerator的使用

    迭代器模式是设计模式中行为模式(behavioral pattern)的一个例子,他是一种简化对象间通讯的模式,也是一种非常容易理解和使用的模式.简单来说,迭代器模式使得你能够获取到序列中的所有元素而 ...

  5. Linux——ps(列出进程)

    ps是Linux系统中用于查看进程状况的命令,用于显示当前系统中进程的快照.ps会显示部分当前活动的进程信息,不同于top指令,top指令会实时的更新所显示的进程动态. Linux的ps指令兼容了多种 ...

  6. Java虚拟机性能管理神器 - VisualVM(3) 插件安装与更新路径配置

    Java虚拟机性能管理神器 - VisualVM(3)  插件安装与更新路径配置 插件路径地址配置方法: VisualVM打开后,会发现功能比较单一,只有概述.监视.线程.抽样器.Profiler五个 ...

  7. mysql错误代码对照表较完整 mysql_errno()

    From: http://blog.csdn.net/aidenliu/article/details/5925604 mysql错误代码对照表较完整  0101 属于其他进程的专用标志. 0102 ...

  8. sparkSQL实际应用

    提交代码包 /usr/local/spark/bin$ spark-submit --class "getkv" /data/chun/sparktes.jar 1.查询KV im ...

  9. php 区分0和空

    能够区分出来的有2,4,6 方法 public function test(){ $test=; if($test==''){ echo '<br />在php中1,0即为空'; //被输 ...

  10. (转)Java程序员应该知道的10个调试技巧

    (转自 酷勤网 – 程序员的那点事!http://www.kuqin.com/) 试可以帮助识别和解决应用程序缺陷,在本文中,作者将使用大家常用的的开发工具Eclipse来调试Java应用程序.但这里 ...