There are two sorted arrays nums1 and nums2 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)).
解题思路:合并两个数组,创建一个 Map对象,用以存放排好顺序的键值对,键为序号,值为数组值,中位数的结果分两种情况讨论:
1、m+n为奇数:(m+n)/2为中位数
2、m+n为偶数:(((m+n)/2-1)+(m+n)/2)/2为中位数
public class FindMedianNum {
public static void main(String[] args) {
int[] a = {1,2,3,4,5,10};
int[] b = {3,5,6,7};
double median = findMedianNum(a,b);
System.out.println(median);
}
public static double findMedianNum(int[] a, int[] b){
int m = a.length;
int n = b.length;
int i = 0, j =0, k = 0;
if(m==1 && n == 0){
return a[0];
}
if(m == 0 && n == 1){
return b[0];
}
Map<Integer, Integer> map = new HashMap<Integer,Integer>();
while(i < m && j < n){
if(a[i] <= b[j]){
map.put(k, a[i]);
++i;
++k;
}else{
map.put(k, b[j]);
++j;
++k;
}
}
while(i < m){
map.put(k, a[i]);
++i;
++k;
}
while(j < n){
map.put(k, b[j]);
++j;
++k;
}
if((m+n)%2 == 0){
return (double)(map.get((m+n)/2-1) + map.get((m+n)/2))/2;
}else{
return (double)map.get((m+n)/2);
}
}
}
There are two sorted arrays nums1 and nums2 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)).的更多相关文章
- 给定两个有序整数数组 nums1 和 nums2,将 nums2 合并到 nums1 中,使得 num1 成为一个有序数组
题目描述: 给定两个有序整数数组 nums1 和 nums2,将 nums2 合并到 nums1 中,使得 num1 成为一个有序数组. 说明:初始化 nums1 和 nums2 的元素数量分别为 m ...
- 给定两个大小为 m 和 n 的有序数组 nums1 和 nums2。 请你找出这两个有序数组的中位数,并且要求算法的时间复杂度为 O(log(m + n))。 你可以假设 nums1 和 nums2 不会同时为空。
class Solution { public double findMedianSortedArrays(int[] A, int[] B) { int m = A.length; int n = ...
- No.004:Median of Two Sorted Arrays
问题: There are two sorted arrays nums1 and nums2 of size m and n respectively.Find the median of the ...
- [LeetCode] Median of Two Sorted Arrays 两个有序数组的中位数
There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...
- 【leedcode】 Median of Two Sorted Arrays
https://leetcode.com/problems/median-of-two-sorted-arrays/ There are two sorted arrays nums1 and num ...
- leetcode-【hard】4. Median of Two Sorted Arrays
题目 There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the ...
- Leetcode4:Median of Two Sorted Arrays@Python
There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...
- leetcode 4. Median of Two Sorted Arrays
https://leetcode.com/problems/median-of-two-sorted-arrays/ There are two sorted arrays nums1 and num ...
- LeetCode:4_Median of Two Sorted Arrays | 求两个排序数组的中位数 | Hard
题目: There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the ...
随机推荐
- jrebel配置热部署参数
jrebel配置热部署参数: -noverify -agentpath:D:/jrebel/lib/jrebel64.dll -Drebel.dirs=E:/workspace/item/src/ma ...
- Hibernate执行原生态sql语句
@Override public Integer update(String id, String username){ String sql = "update Team_CheLiang ...
- EventBus In eShop -- 解析微软微服务架构Demo(四)
引言 大家好像对分析源码厌倦了,说实在我也会厌倦,不过不看是无法分析其后面的东西,从易到难是一个必要的过程. 今天说下EventBus,前几天园里的大神已经把其解刨,我今天就借着大神的肩膀,分析下在e ...
- 我眼中的ASP.NET Core之微服务
### 前言 前几天在博客园看到有园友在分享关于微软的一个微服务架构的示例程序,想必大家都已经知道了,那就是[eShopOnContainers](https://github.com/dotnet- ...
- 进程间通信之AIDL
一.引言 AIDL是android内部进程通信接口的描述语言,是实现跨进程方法调用的一大利器,其中Binder和Messenger的实现机制都是AIDL. 二.使用下面结合示例说明其使用过程: 本次示 ...
- Hadoop出现的错误及处理
1.local host is: (unknown); destination host is: "yun-ubuntu":8031; 原因:yun-ubuntu这个host 并不 ...
- spa单页面应用(angular)
本篇文章是对单页面的一个简单的基本逻辑操作,这个方法可以搭建基本的单页面的逻辑结构. 简单理解:单页面应用,锚点值切换,一个路由对应一个页面. 路由:此时会创建一个信息保存路由的信息,之后对页面a标签 ...
- Pandas数据处理实战:福布斯全球上市企业排行榜数据整理
手头现在有一份福布斯2016年全球上市企业2000强排行榜的数据,但原始数据并不规范,需要处理后才能进一步使用. 本文通过实例操作来介绍用pandas进行数据整理. 照例先说下我的运行环境,如下: w ...
- Maven-FAQ
1.Failure to transfer org.apache.maven.plugins:maven-resources-plugin:pom:2.5...: Q: 第一次使用maven+ecli ...
- Frameset框架集的应用
Frameset框架集常用于写网站后台页面,大多数"T字型"布局后台页面,就是应用Frameset框架集来做的.Franeset框架集的优点是,他可以在同浏览器窗口显示不同页面内容 ...