求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. Dreamwaver 使用root用户连接不上远程服务器

    我用dreamweaver连接远程服务,开始用的是root用户登录的,但是连接不上.网上查了一下,解决教程非常复杂,我就不列出来了. 后来我想了一下,之前我有连接过.我感觉可能是用户的问题,于是我在远 ...

  2. VS 2003 无法打开Web项目 文件路径与URL不符 这两者需要映射到相同的服务器位置

    解决方法: 将C:\Documents   and   Settings\Administrator\VSWebCache下面的文件全部删除

  3. 不到30行JS代码实现的Excel表格

    不到30行JS代码实现的Excel表格,jQuery并非不可替代 某国外程序员展示了一个由原生JS写成不依赖第三方库的,Excel表格应用,有以下特性: 由不足30行的原生JavaScript代码实现 ...

  4. linux crontab运行

    Linux在相应用户下,用crontab -l 命令可以查看该用户定时执行的任务. 1- $>crontab -l 无内容. 则表示没有指定用户执行对应用户下的crontab文件. 2- $&g ...

  5. 网易2014校园招聘杭州Java笔试题

    10) ABC http://soft.chinabyte.com/os/56/12516056.shtml 11) BD. 12) AC. http://blog.sina.com.cn/s/blo ...

  6. HDU 1180 诡异的楼梯(BFS)

    诡异的楼梯 Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status ...

  7. TreeSet与HashSet的区别

    Set是java中一个不包含重复元素的collection.更正式地说,set 不包含满足 e1.equals(e2) 的元素对 e1 和 e2,并且最多包含一个 null 元素.正如其名称所暗示的, ...

  8. BZOJ 2301 Problem B(莫比乌斯反演)

    http://www.lydsy.com/JudgeOnline/problem.php?id=2301 题意:给a,b,c,d,k,求gcd(x,y)==k的个数(a<=x<=b,c&l ...

  9. 手机三种SIM卡 你所不知道的剪卡“秘密”

    SIM卡物理尺寸的发展是逐渐轻薄化,尺寸逐渐缩小的一个过程,最早手机中的卡都是2FF,2003年国际标准提出3FF,当前很多终端都使用这种形态的卡,4FF在2011年的国际标准会议中提出,2012年纳 ...

  10. C# 伪造 referer 提交数据

    private string SendRequest(string account, string cardNumber, string cardPass) { string targetUrl = ...