题目链接: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. Python——thread

    该模块在Python 3中更名为 _thread. 该模块实现对Python多线程的低层次操作原语,也提供了多线程之间用于同步的锁机制.threading 模块基于本模块提供了更易用的高层次线程API ...

  2. ckeditor4.2.1常用配置

    常用的配置信息都放在根目录的config.js文件中,一些常用的配置项如下: 1.界面语言 //语言 config.language = 'zh-cn'; 2.字体添加中文 //字体 config.f ...

  3. (个人)CentOS的JDK安装

    一. 卸载JDK 1. 通过如下命令查看当前JDK的相关内容 rpm -qa | grep java 2. 如果出现如下内容 java-1.7.0-openjdk-1.7.0.9-2.3.4.1.el ...

  4. LAMP架构介绍MySQL、MariaDB介绍 MySQL安装

  5. spark1.4配置安装

    https://segmentfault.com/a/1190000004508993

  6. 基于PHP采集数据入库程序(一)

    前几天有一朋友要我帮做一个采集新闻信息的程序,抽了点时间写了个PHP版本的,随笔记录下. 说到采集,无非就是远程获取信息->提取所需内容->分类存储->读取->展示 也算是简单 ...

  7. linux command not found错误提示

    错误提示:screen: command not found, 如果提示screen: command not found 命令不存在可以执行:yum install screen 或 apt-get ...

  8. SharePoint 2013 将HTML文件转换为母版页

    内容中包含 base64string 图片造成字符过多,拒绝显示

  9. MSM--Memcached_Session_Manager介绍及使用

    MSM--Memcached_Session_Manager介绍及使用 http://www.iteye.com/topic/1125301 我们都知道对于一些大型的web2.0的网站,在正式部署时一 ...

  10. Node.js 模块之 morgan中间件记录日志

    NodeJs中Express框架使用morgan中间件记录日志 Express中的app.js文件已经默认引入了该中间件var logger = require('morgan'); 使用app.us ...