解题思路:合并两个数组,创建一个 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. ASP.NET Core 源码学习之 Options[3]:IOptionsSnapshot

    在 上一章 中,介绍了 IOptions 的使用, 而我们知道,在ConfigurationBuilder的AddJsonFile中,有一个reloadOnChange参数,设置为true时,在配置文 ...

  2. linux服务器查看redis版本:

    linux服务器查看redis版本:redis-server-v

  3. 限制input[type=number]的输入位数策略整理

    当我们使用类型number的input输入框的时候,我们可能需要限制输入的位数,这个时候通常会想到maxlength,但是maxlength是在number类型的时候是不支持的,下面是一些解决这种问题 ...

  4. svn怎么切换用户

    在使用svn客户端经常会保存用户及密码,方便下次直接登录,有时需要使用别的账号登录,这时该怎么切换呢? 工具/原料 苹果6 小米4 方法/步骤   在使用svn更新或提交数据时需要输入用户名和密码,在 ...

  5. 如何在自己的网页上插入一个超链接,发起临时qq会话

    1.先开通临时会话功能 打开网页http://shang.qq.com/v3/index.html

  6. peoplesoft function PSTREENODE 通过 deptid 获得部门树 全路径 code

    create or replace function getUnitFullcode(deptid in varchar) return varchar2 is r ); c int; n ); m ...

  7. Tomcat Connector的三种运行模式

    详情参考: http://tomcat.apache.org/tomcat-7.0-doc/apr.html http://www.365mini.com/page/tomcat-connector- ...

  8. Linux之虚拟机网络配置

    一般安装完虚拟机后,VMware会为虚拟机在网络连接配置为“NAT模式(N):用于共享主机的IP地址”. 这种模式下虚拟机会共享主机的网络环境,主机可以访问外网那么虚拟机可以,主机可以(哪怕是拨VPN ...

  9. 解决CentOS7中文乱码(包括Tomcat日志乱码)问题

    Linux系统中文语言乱码,是很多小伙伴在开始接触Linux时经常遇到的问题,而且当我们将已在Wndows部署好的项目搬到Linux上运行,Tomcat的输出日志中文全为乱码(在Windows上正常) ...

  10. poj3320 (尺取法)

    n个数,求最小区间覆盖着n个数中所有的不相同的数字. 解题思路: AC代码: import java.util.HashMap; import java.util.HashSet; import ja ...