题目链接: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. jQuery AJAX中文乱码处理

    最近工作中用jQuery ajax返回出现乱码,用的Notepad++编辑器,当JS部分传递中文时,另一页面接收的话会出现乱码,在网上找了很多方法,基本上没有很好的解决. 页面用GB2312编码,JS ...

  2. Unity导入3D模型的过程与方法

    一.介绍 资源是游戏开发中的原材料,也就是组成游戏的模块. Unity只是一个游戏开发引擎,而并不是一个资源开发软件.这就意味着在游戏中需要的资源通常是由一些设计者使用其他软件开发出来的,然后设计者会 ...

  3. Linux 下查看某个进程运行的堆栈信息

    1. 根据进程名称查询进程ID ps -ef | grep processName 2. 将进程的堆栈信息写入log gstack processId > s.log 3. 查看log vim ...

  4. JDBC排序数据实例

    在本教程将演示如何在JDBC应用程序中,从数据库表中查询数据记录,在查询语句中将使用asc和desc关键字按升序或降序对记录进行排序.在执行以下示例之前,请确保您已经准备好以下操作: 具有数据库管理员 ...

  5. tpshop全局公共方法

    TPshop 全局公告函数库 前后台可用  这里只列出有哪些方法, 简单描述, 具体的函数体查看 Application\Common\Common\function.php 文件 <?php ...

  6. django学习2 视图和模板

    1 编写更多的视图 polls/views.py def detail(request, question_id): return HttpResponse("You're looking ...

  7. tushare使用

    tushare是获取行情数据的一款免费软件 使用方法很简单, 先安装:pip install tushare 然后 import tushare as ts 即可.   data = ts.get_k ...

  8. CURL 常用参数

    在Linux中curl是一个利用URL规则在命令行下工作的文件传输工具,可以说是一款很强大的http命令行工具. 1.查看响应头信息: -I :显示http response的头信息. [root@l ...

  9. C# 把string字符导出到txt文档方法

    public static string writtxt(string html, string file) { FileStream fileStream = new FileStream(Envi ...

  10. yii2 使用阿里大鱼短信

    1.首先申请阿里账号 2.开通短信服务 3.短信签名 4.添加模板 以上4步是前期工作 -------------------------------------------------------- ...