1 题目

There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).

2 思路

这题比较简单,实现的其实就是归并排序merge那个部分。

3 代码

public class MedianOfTwoSortedArrays {
public double findMedianSortedArrays(int A[], int B[]) {
double median = 0;
int sum = A.length + B.length;
int[] C = new int[sum];
int c = 0;
int a = 0;
int b = 0;
while(a < A.length && b < B.length){
if (A[a] > B[b]) {
C[c] = B[b];
b++;
c++;
}else {
C[c] = A[a];
a++;
c++;
}
}
while (a < A.length) {
C[c] = A[a];
c++;
a++;
}
while(b < B.length){
C[c] = B[b];
c++;
b++;
} if (sum % 2 == 0) {
median = (double) (C[sum/2] + C[sum/2 - 1])/2;
}else {
median = C[sum/2];
}
System.out.println(median);
return median;
}

[leet code 4] Median of Two Sorted Arrays的更多相关文章

  1. 2.Median of Two Sorted Arrays (两个排序数组的中位数)

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

  2. 【转载】两个排序数组的中位数 / 第K大元素(Median of Two Sorted Arrays)

    转自 http://blog.csdn.net/zxzxy1988/article/details/8587244 给定两个已经排序好的数组(可能为空),找到两者所有元素中第k大的元素.另外一种更加具 ...

  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. 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 ...

  5. (python)leetcode刷题笔记04 Median of Two Sorted Arrays

    4. Median of Two Sorted Arrays There are two sorted arrays nums1 and nums2 of size m and n respectiv ...

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

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

  7. [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 ...

  8. No.004 Median of Two Sorted Arrays

    4. Median of Two Sorted Arrays Total Accepted: 104147 Total Submissions: 539044 Difficulty: Hard The ...

  9. LeetCode(3) || Median of Two Sorted Arrays

    LeetCode(3) || Median of Two Sorted Arrays 题记 之前做了3题,感觉难度一般,没想到突然来了这道比较难的,星期六花了一天的时间才做完,可见以前基础太差了. 题 ...

随机推荐

  1. 杭电1518 Square(构成正方形) 搜索

    HDOJ1518 Square Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  2. 使用flask-alchemy 过程中报错KeyError: 'SQLALCHEMY_TRACK_MODIFICATIONS'

    在网上找了很多, 大多数说是必须要给 SQLALCHEMY_TRACK_MODIFICATIONS 一个默认值,尝试修改alchemy 源码,,但是还是不起作用 最后阅读源码 , self.app = ...

  3. Eclipse编辑XML自动提示(zz)

    Eclipse编辑XML自动提示 博客分类: j2se XMLEclipseiBATISSpringSQL  IED Eclipse Java EE IDE for Web Developers: D ...

  4. delphi 中的win32 以外到平台的字符串处理一定慢吗?(转载)

    原始连接:http://rvelthuis.blogspot.tw/2018/01/strings-on-other-platforms-than-32-bit.html Strings too sl ...

  5. 核心一:DI

    1.DI:中文名称:依赖注入 2.英文名称:(Dependency Injection) 3.DI是什么?? 3.1 DI和IoC是一样的 3.2 当一个类(A)中需要依赖另一类(B)对象时,把B赋值 ...

  6. ORACLE rollup函数

    rollup函数应用场景: 主要使用在 分组中,将每个分组求汇总值(就是小计),最后再讲所有值(除去小计)求和(就是合计) 当然,使用union 也可以达到同样的效果.先将需要查询的分组查出来,再un ...

  7. SELECT INTO创建临时表

    SELECT INTO创建临时表 SQL Server临时表有两种类型:本地和全局.它们在名称.可见性以及可用性上有区别.本地临时表的名称以单个数字符号 (#) 打头:它们仅对当前的用户连接是可见的: ...

  8. ac自动机板子

    hdu2222 #include<bits/stdc++.h> #define ll long long #define M 500005 using namespace std; int ...

  9. Win7 MinGW环境测试SDL2.0.3

    下载MinGW版的文件 http://www.libsdl.org/release/SDL2-devel-2.0.3-mingw.tar.gz 解压放到mysys下面 运行Makefile mysys ...

  10. SQL中的split方法的使用

    参数说明: 1.@String :需要split的字符串 2.@Delimiter :格式化时分隔符 3.@index :返回split后数组的值 ), ),)) ) AS BEGIN )) ) DE ...