解题思路:合并两个数组,创建一个 Map对象,用以存放排好顺序的键值对,键为序号,值为数组值,中位数的结果分两种情况讨论:

  1、m+n为奇数:(m+n)/2为中位数

  2、m+n为偶数:(((m+n)/2-1)+(m+n)/2)/2为中位数

public class FindMedianNum {
  public static void main(String[] args) {
    int[] a = {1,2,3,4,5,10};
    int[] b = {3,5,6,7};
    double median = findMedianNum(a,b);
    System.out.println(median);
  }
  public static double findMedianNum(int[] a, int[] b){
    int m = a.length;
    int n = b.length;
    int i = 0, j =0, k = 0;
    if(m==1 && n == 0){
      return a[0];
    }
    if(m == 0 && n == 1){
      return b[0];
    }
    Map<Integer, Integer> map = new HashMap<Integer,Integer>();
    while(i < m && j < n){
      if(a[i] <= b[j]){
        map.put(k, a[i]);
        ++i;
        ++k;
      }else{
        map.put(k, b[j]);
        ++j;
        ++k;
      }
    }
    while(i < m){
      map.put(k, a[i]);
      ++i;
      ++k;
    }
    while(j < n){
      map.put(k, b[j]);
      ++j;
      ++k;
    }
    if((m+n)%2 == 0){
      return (double)(map.get((m+n)/2-1) + map.get((m+n)/2))/2;
    }else{
      return (double)map.get((m+n)/2);
    }
  }
}

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)).的更多相关文章

  1. 给定两个有序整数数组 nums1 和 nums2,将 nums2 合并到 nums1 中,使得 num1 成为一个有序数组

    题目描述: 给定两个有序整数数组 nums1 和 nums2,将 nums2 合并到 nums1 中,使得 num1 成为一个有序数组. 说明:初始化 nums1 和 nums2 的元素数量分别为 m ...

  2. 给定两个大小为 m 和 n 的有序数组 nums1 和 nums2。 请你找出这两个有序数组的中位数,并且要求算法的时间复杂度为 O(log(m + n))。 你可以假设 nums1 和 nums2 不会同时为空。

    class Solution { public double findMedianSortedArrays(int[] A, int[] B) { int m = A.length; int n = ...

  3. No.004:Median of Two Sorted Arrays

    问题: There are two sorted arrays nums1 and nums2 of size m and n respectively.Find the median of the ...

  4. [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 two ...

  5. 【leedcode】 Median of Two Sorted Arrays

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

  6. leetcode-【hard】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 ...

  7. Leetcode4:Median of Two Sorted Arrays@Python

    There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...

  8. leetcode 4. Median of Two Sorted Arrays

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

  9. LeetCode:4_Median of Two Sorted Arrays | 求两个排序数组的中位数 | Hard

    题目: There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the ...

随机推荐

  1. json转javascript对象

    json转javascript对象var tpIdObj = eval("(" + tpid + ")");//json字符串转 对象var tpid = JS ...

  2. 合并静态库出现 can't move temporary file错误

    静态库的制作就不说了很简单,网上也很多例子,这里主要讲下我合并通用静态库时候遇见的坑,在合并前注意.a文件一定要正确,我有一次scheme选了release但是device忘了换,结果怼着两个模拟器静 ...

  3. 从Java虚拟机的内存区域、垃圾收集器及内存分配原则谈Java的内存回收机制

    一.引言: 在Java中我们只需要轻轻地new一下,就可以为实例化一个类,并分配对应的内存空间,而后似乎我们也可以不用去管它,Java自带垃圾回收器,到了对象死亡的时候垃圾回收器就会将死亡对象的内存回 ...

  4. mysqlclient和PyMySQL对比

    环境:Python 3.5+, Django 1.9+ 最初用django时,搜索时发现PyMySQL的文章很多,然而在django的官方文档中python3版的mysql客户端驱动确没有提到PyMy ...

  5. 【原创】源码角度分析Android的消息机制系列(六)——Handler的工作原理

    ι 版权声明:本文为博主原创文章,未经博主允许不得转载. 先看Handler的定义: /** * A Handler allows you to send and process {@link Mes ...

  6. 分享几个 git 的使用场景

    你真的会使用 git 吗?你能回答下面几个问题吗? 有三个commit(顺序:CommitA.CommitB.CommitC),它们相互独立,没有依赖. 在不修改B.C的前提下,修改A,怎么操作? 合 ...

  7. 2.如何实现使用VBS脚本程序对直播间自动评论

    前言:本文使用的是VBS脚本,实现了对繁星直播自动登录,自动进入房间并且自动评论. 前提准备:把需要刷的评论放到mysql中,再使用vbs读出评论 -------------------------- ...

  8. Linux之正则表达式

    正则表达式与通配符的区别: 最常应用正则表达式的命令是grep(egrep),sed,awk. 正则表达式和通配符有本质区别,正则表达式用来找:[文件]内容,文本,字符串.一般只有三剑客支持.通配符用 ...

  9. Ext 常用组件解析

    Ext 常用组件解析 Panel 定义&常用属性 //1.使用initComponent Ext.define('MySecurity.view.resource.ResourcePanel' ...

  10. python迭代器生成器(二)

    其他内置类型迭代器 除了文件以及列表这样的实际的序列外,其他类型也有适合的迭代器. 遍历字典的经典方法是明确的获取其键的列表. 在最近的python版本中,字典有一个迭代器,在迭代环境中,会自动一次返 ...