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 ...
随机推荐
- Oracle之DBMS_SQL包用法详解
对于一般的(select)操作,如果使用动态的sql语句则需要进行以下几个步骤:open cursor--->parse---> bind variable ---> defi ...
- Group Layout
----------------siwuxie095 将根面板 contentPane 的布局切换为 Group Layout Grou ...
- C++结构体的定义、初始化和引用
定义: 结构体(struct)是由一系列具有相同类型或不同类型的数据构成的数据集合,也叫结构. 声明一个结构体类型的形式是: struct Student{ //声明一个结构体类型Student in ...
- Angular01 利用grunt搭建自动web前端开发环境、利用angular-cli搭建web前端项目
搭建angular开发环境 一.下载并安装node 官网地址:点击前往 二.利用npm安装cnpm 安装好node后就可以使用npm命令啦 查看版本:npm -v 安装cnpm:npm install ...
- ROS Learning-020 learning_tf-04(编程)让turtle2 海龟跟随turtle1海龟,并绕着 turtle1海龟转圈 (Python版)
ROS Indigo learning_tf-04 (编程)让 turtle2 海龟跟随 turtle1 海龟,并绕着 turtle1 海龟转圈 (Python版) 我使用的虚拟机软件:VMware ...
- 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-003比较算法及算法的可视化
一.介绍 1. 2. 二.代码 1. package algorithms.elementary21; /*********************************************** ...
- CF321E Ciel and Gondolas & BZOJ 5311 贞鱼
一眼可以看出$O(kn^{2})$的$dp$方程,然后就不会了呜呜呜. 设$f_{i, j}$表示已经选到了第$i + 1$个数并且选了$j$段的最小代价,那么 $f_{i, j} = f_{p, j ...
- backstop无法访问
解决方案:重新build代码,重新启动虚拟机.再等一会儿,就OK了.
- Umbraco 中获取一个media item的文件路径 file path
我们要使用UmbracoHelper, 这里就需要用到我们在之前的blog里面写的UmbracoContext 参看这个blog https://www.cnblogs.com/wphl-27 ...
- java全栈day01-01
一 常用dos命令d:dir:列出当前目录下的文件以及文件夹md 创建目录rd 删除目录cd 进入指定的目录cd\ 返回根目录del a.txtdel *.txt 删除所有文件exit :退出dos ...