Median of Two Sorted Arrays(Java)
求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)的更多相关文章
- 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 ...
- 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 ...
- LeetCode 4 Median of Two Sorted Arrays (两个数组的mid值)
题目来源:https://leetcode.com/problems/median-of-two-sorted-arrays/ There are two sorted arrays nums1 an ...
- Kotlin实现LeetCode算法题之Median of Two Sorted Arrays
题目Median of Two Sorted Arrays(难度Hard) 方案1,数组合并&排序调用Java方法 import java.util.* class Solution { fu ...
- 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 ...
- 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 ...
- 【算法之美】求解两个有序数组的中位数 — leetcode 4. Median of Two Sorted Arrays
一道非常经典的题目,Median of Two Sorted Arrays.(PS:leetcode 我已经做了 190 道,欢迎围观全部题解 https://github.com/hanzichi/ ...
- [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 ...
- 2.Median of Two Sorted Arrays (两个排序数组的中位数)
要求:Median of Two Sorted Arrays (求两个排序数组的中位数) 分析:1. 两个数组含有的数字总数为偶数或奇数两种情况.2. 有数组可能为空. 解决方法: 1.排序法 时间复 ...
随机推荐
- 为什么报错说req未定义,createServer只接受匿名函数吗?
var http = require('http');var server = new http.createServer(handlerRequest(req,res));server.listen ...
- JQuery 之事件中的 ----- hover 与 onmouseover 、onmouseout 联系
hover([over,]out) 一个模仿悬停事件(鼠标移动到一个对象上面及移出这个对象)的方法.这是一个自定义的方法,它为频繁使用的任务提供了一种“保持在其中”的状态. 当鼠标移动到一个匹配的元素 ...
- js Range
http://www.zhangxinxu.com/wordpress/2011/04/js-range-html%E6%96%87%E6%A1%A3%E6%96%87%E5%AD%97%E5%86% ...
- TCP 和 UDP 协议发送数据包的大小 (转载)
MTU最大传输单元,这个最大传输单元实际上和链路层协议有着密切的关系,EthernetII帧的结构DMAC+SMAC+Type+Data+CRC由于以太网传输电气方面的限制,每个以太网帧都有最小的大小 ...
- Python学习笔记6(列表生成式)
1.生成列表 要生成list [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],我们可以用range(1, 11): >>> range(1, 11) [1, 2, 3 ...
- 写一个Windows上的守护进程(7)捕获异常并生成dump
写一个Windows上的守护进程(7)捕获异常并生成dump 谁都不能保证自己的代码不出bug.一旦出了bug,最好是崩溃掉,这样很快就能被发现,若是不崩溃,只是业务处理错了,就麻烦了,可能很长时间之 ...
- 初识KMP
KMP简介 KMP是一种由Knuth(D.E.Knuth).Morris(J.H.Morris)和Pratt(V.R.Pratt)设计的字符串匹配算法.对目标串T[0:n-1]中查找与之匹配的模式串P ...
- C语言实现界面(不通过MFC\避免遗忘)
感觉MFC不属于程序员细究的东西,今实现基本界面避免日后遗忘. 源代码: #include<windows.h>#include<stdio.h>char str[] = {' ...
- java版-JQuery上传插件Uploadify使用实例
摘自:http://itindex.net/detail/47160-java-jquery-%E4%B8%8A%E4%BC%A0 运行效果: 包结构图: 后台JAVA逻辑: package com. ...
- linux下挂载CDROM命令
cdrom装载在/dev/cdrom 下面. 挂载时在权限范围内建立一个文件夹.例如mkdir /home/dang/cdtmp 然后执行 mount dev/cdrom /home/dang/cdt ...