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 ...
随机推荐
- Java 基础 变量介绍
变量的声明和使用 概念: 变量是指内存中的一个存储区域,该区域要有自己的名称(变量名).类型(数据类型),该区域的数据可以在同一数据类型的范围内不断变化值: 变量的使用注意事项: Java中的变量必须 ...
- 【Android Developers Training】 101. 显示快速联系人挂件(Quick Contact Badge)
注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...
- 门控开关项目--使用Multism仿真
可以修改桥式整流电路的电容,发现容值不合适的时候,直流不平稳.
- keyStore很重要,千万不能丢失
打包apk的时候需要对apk文件进行签名,如果想要自己给apk签名那么就要自己创建keystore.1.签名的意义为了保证每个应用程序开发商合法ID,防止部分开放商可能通过使用相同的Package N ...
- golang的并发不等于并行
先 看下面一道面试题: func main() { runtime.GOMAXPROCS(1) wg := sync.WaitGroup{} wg.Add(20) for i := 0; i < ...
- Vijos 1001 谁拿了最多奖学金
题目描述 某校的惯例是在每学期的期末考试之后发放奖学金.发放的奖学金共有五种,获取的条件各自不同: 1) 院士奖学金,每人8000元,期末平均成绩高于80分(>80),并且在本学期内发表1篇或1 ...
- (转载)oracle 在一个存储过程中调用另一个返回游标的存储过程
原文链接:http://www.jb51.net/article/20160.htm 实际项目当中经常需要在一个存储过程中调用另一个存储过程返回的游标,本文列举了两种情况讲述具体的操作方法. 第一种情 ...
- mysql基础之yum安装mysql5.7.18
2017-04-19 一.实验环境 centos7_x64 由于centos7的yum源里默认使用了mariadb替代了mysql,所有我们还得先配置一下yum源.当然mariadb和mysql是兼容 ...
- Java 异常处理笔记
Java程序运行过程中所发生的异常事件可分为两类: §错误(Error):JVM系统内部错误.资源耗尽等严重情况 §违例(Exception): 其它因编程错误或偶然的外在因素导致的一般性问题,例如: ...
- PostMessage,模拟键盘输入
for i := 0 to length(tel) do begin //SendMessage(cHwnd,WM_KEYDOWN,Integer(tel[ ...