解题思路:合并两个数组,创建一个 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. 数据结构与算法(c++)——查找二叉树与中序遍历

    查找树ADT--查找二叉树 定义:对于树中的每个节点X,它的左子树中的所有项的值小于X中的项,而它的右子树中所有项的值大于X中的项. 现在给出字段和方法定义(BinarySearchTree.h) # ...

  2. 使用CoApp创建NuGet C++静态库包

    NuGet是微软开发平台下的包管理软件,使用它你可以非常方便的将一些第三方的库.框架整合进自己的项目中,省去了不少麻烦的配置过程.但是从官方文档上来看,貌似NuGet对C++的支持不是很好,并且在现阶 ...

  3. 如何离线安装Visual Studio 2017

    1. 官方下载在线安装文件 vs_community.exe https://www.visualstudio.com/zh-hans/thank-you-downloading-visual-stu ...

  4. Android 音乐播放

    android简单音乐播放控制代码 这个几个月业余时间一直在做一个android项目,里面涉及到了音乐播放功能.很简单那种,播放.暂停.上一曲.下一曲.音量调节等. 音乐播放主要使用的对象是Media ...

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

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

  6. 【操作教程】SequoiaDB分布式存储教程

    1.各模式适用场景介绍 由于SequoiaDB对比其他的NoSQL有更多的方式将数据分布到多台服务器上,所以下面笔者为阅读者一一介绍每种分布式方式适合于哪种场景. 1.1 Hash 方式分布数据 在H ...

  7. Echarts笔记——使用AJAX填充数据

    最近把编辑器从Sublime换成HBuilder,感觉好用很多啊,可能自己插件没弄好吧.不不过HBuilder的启动速度确实慢,放机械盘启动要7-13秒,还好有固态. 因为项目需要,这周上手了百度的E ...

  8. JS之正则表达式

    一.正则表达的目标: 1.使用表单事件和脚本函数实现表单验证 2.使用String对象和文本框控件常用属性和方法实现客户端验证 二.什么需要表单验证: 1.表单元素是否为空 2.用户名和密码 3.E- ...

  9. ReactiveCocoa源码解析(六) SignalProtocol的take(first)与collect()延展实现

    上篇博客我们聊了observe().map().filter()延展函数的具体实现方式以及使用方式.我们在之前的博客中已经聊过,Signal的主要功能是位于SignalProtocol的协议延展中的, ...

  10. 选择、冒泡排序,二分查找法以及一些for循环的灵活运用

    import java.util.Arrays;//冒泡排序 public class Test { public static void main(String[] args) { int[] ar ...