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.排序法 时间复 ...
随机推荐
- js获取 input file 图片缩略图
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- (转)ASP.Net 学习路线
入门篇 1.学习面向对象(OOP)的编程思想 许多高级语言都是面向对象的编程,.NET也不例外.如果您第一次接触面向对象的编程,就必须理解类.对象.字段.属性.方法和事件.封装.继承和多态性.重载.重 ...
- asp.net 读取Excel文档
<注:>默认读取数据从Excel的第二行开始. public DataSet ReadExcel(string Path) { string strConn = "Provide ...
- SQL Server数据库的操作流程和连接的简单介绍
学习ADO,免不了要跟数据库打交道,对于初学者来说,如果不整理一下整个流程,那么可能会出现很多的问题,下面简单的介绍数据库的操作流程. 1. 我们最终操作的对像是数据表,在操作数据表之前我们先 ...
- JavaScript数组知识网络
JavaScript数据类型 基本数据类型 Boolean Null Number String Symbol Undefined 对象数据类型Object Build-in object Array ...
- EC读书笔记系列之1:条款1、条款2、条款3
条款1:视C++为一个语言联邦 记住: ★C++高效编程守则视状况而变化,这取决于你使用C++的哪一部分 C: Object-oriented c++: Template c++: STL 条款2:尽 ...
- (转)Should I use char** argv or char* argv[]
As you are just learning C, i recommend you to really try to understand the differences between ar ...
- [poj2762] Going from u to v or from v to u?(Kosaraju缩点+拓排)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Going from u to v or from v to u? Tim ...
- js继承模式
组合继承是js常用的继承模式,指的是将原型链和借用构造函数的技术结合在一起.其中的思想是使用原型链实现原型属性和方法的继承, 而通过借用构造函数实现对属性的继承. 例子: <script> ...
- PHP怎么实现网站中,同一个用户不能同时在线?
先上图,看个大概: 一般的原则就是,后一个用户登录时会把前一个用户踢下线. 在用户首次登录时,我们会把用户的sessionid保存到数据库,这个是用户的唯一标识.方便后边操作. 用户只有在登录时才会和 ...