解题思路:合并两个数组,创建一个 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. ES6的开发环境搭建

    在搭建es6开发环境之前,先简单介绍一下es6. ECMAScript 6.0(以下简称 ES6)是 JavaScript 语言的下一代标准,已经在2015年6月正式发布了.它的目标,是使得 Java ...

  2. python list有关remove的问题

    在python 中进行一次简单的列表循环,当用到remove时出现了一个很有趣的现象, 代码如下: a=range(30) for i in a : if i%4!=0: a.remove(i) 这段 ...

  3. [leetcode-611-Valid Triangle Number]

    Given an array consists of non-negative integers, your task is to count the number of triplets chose ...

  4. 【LeetCode】191. Number of 1 Bits

    题目: Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also ...

  5. Chapter 4. The MPEG-4 and H.264 Standards

    本章节介绍一些关于MPEG-4标准与H.264标准的基本知识 比较重要的是第95页关于两种标准的对比表格.其他部分没有什么特别重要的细节.

  6. SQLyog简介

    一.软件简介 SQLyog 是一个快速而简洁的图形化管理MYSQL数据库的工具,它能够在任何地点有效地管理你的数据库.SQLyog是业界著名的Webyog公司出品的一款简洁高效.功能强大的图形化MyS ...

  7. Spring Security -SpEL表达式

    Spring Security -SpEL表达式 开启SpEL表达式 <!-- use-expressions是否开启 SpEL表达式 o.s.s.web.access.expression.W ...

  8. //读取配置文件(属性文件)的工具类-ConfigManager

    package com.pb.news.util; import java.io.IOException;import java.io.InputStream;import java.sql.Resu ...

  9. 英文版windows7中文软件显示乱码的解决办法

    一.打开控制面板,修改语言的归属地为China 修改完成之后重启,一般能解决大部分问题,如果依然有部分显示乱码,就需要去修改注册表

  10. Debian 8添加kali更新源并安装metasploit

    一.Debian 8添加kali更新源 中科大kali更新源: deb http://mirrors.ustc.edu.cn/kali kali-rolling main non-free contr ...