[leet code 4] Median of Two Sorted Arrays
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的更多相关文章
- 2.Median of Two Sorted Arrays (两个排序数组的中位数)
要求:Median of Two Sorted Arrays (求两个排序数组的中位数) 分析:1. 两个数组含有的数字总数为偶数或奇数两种情况.2. 有数组可能为空. 解决方法: 1.排序法 时间复 ...
- 【转载】两个排序数组的中位数 / 第K大元素(Median of Two Sorted Arrays)
转自 http://blog.csdn.net/zxzxy1988/article/details/8587244 给定两个已经排序好的数组(可能为空),找到两者所有元素中第k大的元素.另外一种更加具 ...
- LeetCode 4 Median of Two Sorted Arrays (两个数组的mid值)
题目来源:https://leetcode.com/problems/median-of-two-sorted-arrays/ There are two sorted arrays nums1 an ...
- 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 ...
- (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 ...
- 【算法之美】求解两个有序数组的中位数 — 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 ...
- No.004 Median of Two Sorted Arrays
4. Median of Two Sorted Arrays Total Accepted: 104147 Total Submissions: 539044 Difficulty: Hard The ...
- LeetCode(3) || Median of Two Sorted Arrays
LeetCode(3) || Median of Two Sorted Arrays 题记 之前做了3题,感觉难度一般,没想到突然来了这道比较难的,星期六花了一天的时间才做完,可见以前基础太差了. 题 ...
随机推荐
- Linux 终端设备
<Linux终端设备详解> https://www.cnblogs.com/shineshqw/articles/2423989.html
- 厉害了,他用PS不是P照片而是……
今儿要介绍的主角是战斗民族的设计师 Dmitriy Glazyrin,他这个人用PS做设计有个特点,专门P3D软件做出来的白模. 大家可以想象一下,一个什么颜色什么材质都没有的东西,把它楞是用PS加上 ...
- office 2007,SQL Server 2008,VS2010安装步骤
office 2007,SQL Server 2008,VS2010的安装顺序是不是office 2007,SQL Server 2008,VS2010呢? 前几天先安装了SQL Server 200 ...
- HTTP协议是什么
1.http全称Hypertext Trsnsfer Protocol超文本传输协议 2.最初发明是用来在浏览器和web服务器之间传输超文本信息的 3.泛义上属于应用层的协议 ,很多其他应用(比如 ...
- 找不到类SimpleJdbcTemplate ParameterizedRowMapper cannot be resolved
找不到类SimpleJdbcTemplate 背景 想编译个web应用,原来spring-jdbc.jar用的是Spring 3.1,今天改成用Spring 4.3,报了这个错误. 现象 编译不通过, ...
- ubuntu 安装jdk7小结(转载)
ubuntu 安装jdk7小结 目录(?)[+] ubuntu 安装jdk7,现在来总结一下:第一步:下载jdk-7-linux-i586.tar.gz直接在ORACLE的官网中下载就可以:http: ...
- 【搜索】棋盘问题(DFS)
Description 在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别.要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋盘,摆放k个棋子 ...
- 【Ruby】ruby安装
Ruby简介 Ruby,一种简单快捷的面向对象(面向对象程序设计)脚本语言,在20世纪90年代由日本人松本行弘(Yukihiro Matsumoto)开发,遵守GPL协议和Ruby License.它 ...
- 【Web】网页字体图标的使用
字体图标介绍 网页中图片有很多优点,但也有很多缺点,会增加文件的大小以及增加http请求.这时候就需要用的字体图标(iconfont).字体图标的优点,可以跟图片一样改变透明度.旋转等,本质上是文字, ...
- Django的学习(一)————初入django
一.基本指令 1.项目的建立: Django的项目建立,进入目录,打开cmd输入 django-admin startproject[项目名称],注意如果是在其他文件下把项目设计成资源文件. 2.Ap ...