LeeCode(No4 - 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 sorted arrays. The overall run time complexity should be O(log (m+n)).
Example 1:
nums1 = [1, 3]
nums2 = [2] The median is 2.0
Example 2:
nums1 = [1, 2]
nums2 = [3, 4] The median is (2 + 3)/2 = 2.5
刚看到题基本思路是将两个数据按顺序排列然后取中位数(偶数取中位两数平均数)
下面是自己的方法(姑且称之为齿轮法,也是比较传统的方法):
public double findMedianSortedArrays(int[] nums1, int[] nums2) {
ArrayList<Integer> arrayList = new ArrayList<>();
int index1 = 0;
int index2 = 0;
for (int i =0; i< nums1.length; i++) {
for (int j = index2; j < nums2.length; j++) {
if (nums1[index1] <= nums2[j]) {
arrayList.add(nums1[index1]);
index1 ++;
break;
} else {
arrayList.add(nums2[j]);
index2 ++;
}
}
}
if(index1 < nums1.length) {
for(; index1 < nums1.length; index1++){
arrayList.add(nums1[index1]);
}
}
if(index2 < nums2.length) {
for(; index2 < nums2.length; index2++){
arrayList.add(nums2[index2]);
}
}
int totalSize = arrayList.size();
if(totalSize % 2 == 0) {
return (arrayList.get(totalSize / 2 - 1) + arrayList.get(totalSize / 2)) / 2.0;
} else {
return arrayList.get(totalSize / 2);
}
}
果不其然,性能只打败了13.8%的人。
这个题目的难度是Hard,考虑了许久没有更好解决方案,所以查看官网,果然演变为数学问题,感兴趣的可以看下下面地址
参考:
https://leetcode.com/problems/median-of-two-sorted-arrays/
LeeCode(No4 - Median of Two Sorted Arrays)的更多相关文章
- 求两个有序数组的中位数(4. Median of Two Sorted Arrays)
先吐槽一下,我好气啊,想了很久硬是没有做出来,题目要求的时间复杂度为O(log(m+n)),我猜到了要用二分法,但是没有想到点子上去.然后上网搜了一下答案,感觉好有罪恶感. 题目原型 正确的思路是:把 ...
- 【转载】两个排序数组的中位数 / 第K大元素(Median of Two Sorted Arrays)
转自 http://blog.csdn.net/zxzxy1988/article/details/8587244 给定两个已经排序好的数组(可能为空),找到两者所有元素中第k大的元素.另外一种更加具 ...
- C#LeetCode刷题之#4-两个排序数组的中位数(Median of Two Sorted Arrays)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4005 访问. 给定两个大小为 m 和 n 的有序数组 nums1 ...
- 怎样合并排序数组(How to merge 2 sorted arrays?)
Question: We have 2 sorted arrays and we want to combine them into a single sorted array. Input: arr ...
- 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(二分)
4. Median of Two Sorted Arrays 题目链接:https://leetcode.com/problems/median-of-two-sorted-arrays/ Descr ...
- 【算法之美】求解两个有序数组的中位数 — leetcode 4. Median of Two Sorted Arrays
一道非常经典的题目,Median of Two Sorted Arrays.(PS:leetcode 我已经做了 190 道,欢迎围观全部题解 https://github.com/hanzichi/ ...
- 2.Median of Two Sorted Arrays (两个排序数组的中位数)
要求:Median of Two Sorted Arrays (求两个排序数组的中位数) 分析:1. 两个数组含有的数字总数为偶数或奇数两种情况.2. 有数组可能为空. 解决方法: 1.排序法 时间复 ...
- LeetCode 4 Median of Two Sorted Arrays (两个数组的mid值)
题目来源:https://leetcode.com/problems/median-of-two-sorted-arrays/ There are two sorted arrays nums1 an ...
随机推荐
- day70-oracle PLSQL_01基本语法
PLSQL是一种程序,和java一样都是一种程序. sql developer是基于java的jdbc连接数据库.根据java的jdbc,只要有数据库的驱动,就可以连接这个数据库.这个工具默认不需要任 ...
- 框架之Struts2
相比较hibernate简单了许多 案例:使用Struts2框架完成登录功能 需求分析 1. 使用Struts2完成登录的功能 技术分析之Struts2框架的概述 1. 什么是Struts2的框架 * ...
- POJ 3714 分治/求平面最近点对
第一次见这种问题直接懵圈...没想到分治法这么强大,借鉴了lyd的代码: 代码如下 #include<cstdio> #include<algorithm> #include& ...
- Python 网络爬虫 001 (科普) 网络爬虫简介
Python 网络爬虫 001 (科普) 网络爬虫简介 1. 网络爬虫是干什么的 我举几个生活中的例子: 例子一: 我平时会将 学到的知识 和 积累的经验 写成博客发送到CSDN博客网站上,那么对于我 ...
- Entity Framework Tutorial Basics(18):DBEntityEntry Class
DBEntityEntry Class DBEntityEntry is an important class, which is useful in retrieving various infor ...
- java多线程系列:Semaphore和Exchanger
本篇文章将介绍Semaphore和Exchanger这两个并发工具类. Semaphore 信号量(英语:Semaphore)又称为信号标,是一个同步对象,用于保持在0至指定最大值之间的一个计数值.当 ...
- 国内物联网平台(3):QQ物联智能硬件开放平台
国内物联网平台(3)——QQ物联·智能硬件开放平台 马智 平台定位 将QQ帐号体系.好友关系链.QQ消息通道及音视频服务等核心能力提供给可穿戴设备.智能家居.智能车载.传统硬件等领域的合作伙伴,实现用 ...
- C#socket通信时,怎样判断socket双方是否断开连接
我在Server端new了一个socket,然后bind,开了一个线程来accept前来连接的client,每接到一个client前来连接就新开一个线程和它进行通信.我把Server端得到的socke ...
- 在WinForm中使用Web Services 来实现 软件 自动升级( Auto Update ) (C#)
winform程序相对web程序而言,功能更强大,编程更方便,但软件更新却相当麻烦,要到客户端一台一台地升级,面对这个实际问题,在最近的一个小项目中,本人设计了一个通过软件实现自动升级技术方案,弥补了 ...
- SuperSocket1.6电子书离线版
使用离线浏览器制作,格式为chm,本人不对电子书内容具有任何权利!简体中文,适用于.NET开发. 下载地址