求2个数组的中位数

方法很多

但是时间复杂度各异

1利用数组copy方法先融合两个数组,然后排序,找出中位数

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections; public class _004MedianofTwoSortedArrays { public static void main(String[] args)
{
int a[]={2,3,5,9};
int b[]={1,4,7,10,11};
System.out.println(findMedianSortedArrays(a,b));
} public static double findMedianSortedArrays(int A[], int B[])
{
int[] d3 = new int[A.length + B.length];
System.arraycopy(A, 0, d3, 0, A.length);
System.arraycopy(B, 0, d3, A.length, B.length);
Arrays.sort(d3);
System.out.println(Arrays.toString(d3));
return d3.length%2!=0?d3[d3.length/2]/1.0:(d3[d3.length/2-1]+d3[d3.length/2])/2.0; } }

2是循环判断插入新数组中 等于说不用内置copy方法自己写一个替代

 import java.util.Arrays;

 public class _004_2Median {

     public static void main(String[] args) {
// TODO Auto-generated method stub
int a[]={2,34};
int b[]={1,4,9};
System.out.println(findMedianSortedArrays(a,b));
} private static double findMedianSortedArrays(int[] a, int[] b) {
int k=a.length+b.length;
System.out.println(k);
int[] c=new int[k];
int i=0,j=0,m=0;
while(m!=k)
{
if(i==a.length)
{
c[m]=b[j];
j++; }
else if(j==b.length)
{
c[m]=a[i];
i++;
}
else if(a[i]<b[j])
{
c[m]=a[i];
i++; }
else
{
c[m]=b[j];
j++; }
m++;
} return k%2==0?(c[k/2-1]+c[k/2])/2.0:c[k/2]/1.0; } }

3第三种递归方法有待研究

Median of Two Sorted Arrays(Java)的更多相关文章

  1. Leetcode: Median of Two Sorted Arrays. java.

    There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted ...

  2. leetcode第四题:Median of Two Sorted Arrays (java)

    Median of Two Sorted Arrays There are two sorted arrays A and B of size m and n respectively. Find t ...

  3. LeetCode 4 Median of Two Sorted Arrays (两个数组的mid值)

    题目来源:https://leetcode.com/problems/median-of-two-sorted-arrays/ There are two sorted arrays nums1 an ...

  4. Kotlin实现LeetCode算法题之Median of Two Sorted Arrays

    题目Median of Two Sorted Arrays(难度Hard) 方案1,数组合并&排序调用Java方法 import java.util.* class Solution { fu ...

  5. LeetCode: Median of Two Sorted Arrays 解题报告

    Median of Two Sorted Arrays There are two sorted arrays A and B of size m and n respectively. Find t ...

  6. LeetCode 第四题 Median of Two Sorted Arrays 二人 渣渣选手乱七八糟分析发现基本回到思路1

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

  7. 【算法之美】求解两个有序数组的中位数 — leetcode 4. Median of Two Sorted Arrays

    一道非常经典的题目,Median of Two Sorted Arrays.(PS:leetcode 我已经做了 190 道,欢迎围观全部题解 https://github.com/hanzichi/ ...

  8. [LintCode] 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. 2.Median of Two Sorted Arrays (两个排序数组的中位数)

    要求:Median of Two Sorted Arrays (求两个排序数组的中位数) 分析:1. 两个数组含有的数字总数为偶数或奇数两种情况.2. 有数组可能为空. 解决方法: 1.排序法 时间复 ...

随机推荐

  1. css之浏览器初始化方案

    HTML, body, div, h1, h2, h3, h4, h5, h6, ul, ol, dl, li, dt, dd, p, blockquote,pre, form, fieldset, ...

  2. js实现求平均数功能

    今天在项目中遇到了一个求平均值的需求,大致需求就是,页面上面有四个input框,失去焦点就计算平均值,结果保留两位小数,并输出在页面上.不多说了,直接奉上代码,如有更好思路或者想法,都欢迎大家和我讨论 ...

  3. WebApi官网学习记录---webapi中controller与action的选择

    如果framework找到一个匹配的URI,创建一个包含占位符值的字典,key就是这些占位符(不包括大括号),value来自URI或者默认值,这个字典存储在IHttpRouteData对象中.默认值可 ...

  4. struts2的工作机制

    struts2的工作机制 原文:http://eoasis.iteye.com/blog/642586 概述 本章讲述Struts2的工作原理. 读者如果曾经学习过Struts1.x或者有过Strut ...

  5. MFC让控件随窗口大小而改变

    转载自http://blog.csdn.net/chw1989/article/details/7488711 大小和位置都改变(亲测可行) 1.首先为窗体类添加CRect m_rect,该成员变量用 ...

  6. git操作流程

  7. ZOJ 3745 Salary Increasing

    Description Edward has established a company with n staffs. He is such a kind man that he did Q time ...

  8. basename $0的用法

    basename 从文件名中去掉路径信息, 只打印出文件名. 结构 basename $0 可以让脚本知道它自己的名字, 也就是, 它被调用的名字. 可以用来显示用法信息, 比如如果你调用脚本的时候缺 ...

  9. Log4net 可直接使用的配置

    config配置 <xml version="1.0"> <configuration> <configSections> <!--配置一 ...

  10. Linux系统目录/bin /sbin /usr/bin /usr/sbin和/lib /usrlib的一些分析

    其实就是相当于转载了. /bin,/sbin,/usr/sbin,/usr/bin 目录 这些目录都是存放命令的,首先区别下/sbin和/bin: 从命令功能来看,/sbin 下的命令属于基本的系统命 ...