求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. Chrome 开发者工具详解(4):Profiles 面板

    概述 当前使用的Chrome最新版为54.0.2840.71,这个版本的Profiles面板比之前提供的功能更多也更强大,下面是该面板所包含的功能点: Record JavaScript CPU Pr ...

  2. 被sjy带刷题#1

        笔记[问题描述]给定一个长度为m的序列a,下标编号为1~m.序列的每个元素都是1~n的整数.定义序列的代价为 你现在可以选择两个数x和y,并将序列a中所有的x改成y.x可以与y相等.请求出序列 ...

  3. 我的开源框架之Accordion控件

    需求: (1)实现手风琴面板控件,支持静态HTML与JSON方式创建控件 (2)支持远程加载数据 (3)支持面板激活.远程加载事件注册 (4)支持动态添加.删除项目 实现图例 客户代码 <div ...

  4. 傻瓜式硬盘重装win7系统图文加视频教程

    标准适用环境: win7系统还能用,但是想重装win7系统.[当然win7进不去也可以在PE进行] 其它 需要工具: win7 ISO镜像                          | [ w ...

  5. wordpress教程之文章页single.php获取当前文章所属分类

    之所以要发这篇文章,是因为这个方法适用于: WP默认文章分类 手动添加的自定文章分类 插件(custom post type ui)添加的自定义文章分类(含taxonomy) 方法目的:在文章模板中, ...

  6. BZOJ 2440 完全平方数(莫比乌斯反演,容斥原理)

    http://www.lydsy.com/JudgeOnline/problem.php?id=2440 题意:求第K个没有平方因子的数 思路:首先,可以二分数字,然后问题就转变成x以内有多少无平方因 ...

  7. TextReader/TextWriter 的类

    TextReader以及TextWriter这两个类,非常有用,很多方法都接受它们作为参数. TextReader有两个子类: StringReader/StringWriter 用于读取字符串: S ...

  8. android 签名被篡改(Keystore was tampered with, or password was incorrect)

    在配置自定义签名时出现了"Keystore was tampered with, or password was incorrect"错误! 参考文档发现: If necessar ...

  9. Windows搭建Sublime Text 3 + Go开发环境

    1. 安装Sublime Text 3 Sublime Text 3(以下简称ST)的下载与安装我就不说啦,目前还是一个测试版,不过据说比ST2增加了好多新功能,下载地址: http://www.su ...

  10. UVA_Cubic Eight-Puzzle UVA 1604

    Let's play a puzzle using eight cubes placed on a 3 x 3 board leaving one empty square.Faces of cube ...