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)的更多相关文章

  1. 求两个有序数组的中位数(4. Median of Two Sorted Arrays)

    先吐槽一下,我好气啊,想了很久硬是没有做出来,题目要求的时间复杂度为O(log(m+n)),我猜到了要用二分法,但是没有想到点子上去.然后上网搜了一下答案,感觉好有罪恶感. 题目原型 正确的思路是:把 ...

  2. 【转载】两个排序数组的中位数 / 第K大元素(Median of Two Sorted Arrays)

    转自 http://blog.csdn.net/zxzxy1988/article/details/8587244 给定两个已经排序好的数组(可能为空),找到两者所有元素中第k大的元素.另外一种更加具 ...

  3. C#LeetCode刷题之#4-两个排序数组的中位数(Median of Two Sorted Arrays)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4005 访问. 给定两个大小为 m 和 n 的有序数组 nums1 ...

  4. 怎样合并排序数组(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 ...

  5. 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 ...

  6. Leetcode 4. Median of Two Sorted Arrays(二分)

    4. Median of Two Sorted Arrays 题目链接:https://leetcode.com/problems/median-of-two-sorted-arrays/ Descr ...

  7. 【算法之美】求解两个有序数组的中位数 — leetcode 4. Median of Two Sorted Arrays

    一道非常经典的题目,Median of Two Sorted Arrays.(PS:leetcode 我已经做了 190 道,欢迎围观全部题解 https://github.com/hanzichi/ ...

  8. 2.Median of Two Sorted Arrays (两个排序数组的中位数)

    要求:Median of Two Sorted Arrays (求两个排序数组的中位数) 分析:1. 两个数组含有的数字总数为偶数或奇数两种情况.2. 有数组可能为空. 解决方法: 1.排序法 时间复 ...

  9. LeetCode 4 Median of Two Sorted Arrays (两个数组的mid值)

    题目来源:https://leetcode.com/problems/median-of-two-sorted-arrays/ There are two sorted arrays nums1 an ...

随机推荐

  1. Navicat MySQL安装

    1 下载安装包 点击下载安装包 2 解压安装包(解压后有三个文件) 第一个 Crack 是注册文件 第二个 chinese.rar 是汉化包 第三个 navicat_trial.exe 是安装程序 3 ...

  2. matlab rand(3,5)

    rand()函数在(0,1)上创建均匀分布的随机数的数组 >> rand(3,5) ans = 0.8147 0.9134 0.2785 0.9649 0.9572 0.9058 0.63 ...

  3. 面试题: Struts2

    1. Struts2与Struts1的联系与区别是什么?为什么要用Struts2? 答案: struts1与struts2都是mvc框架的经典实现模式. Struts2不是从Struts1升级而来,而 ...

  4. DPDK内存管理-----rte_mbuf(转)

    本文主要介绍rte_mbuf与rte_mempool数据结构之间的组织关系.以及网卡接收到的数据是如何存储在rte_mbuf中的. 一.rte_mbuf.rte_mempool及网卡收到的数据包在内存 ...

  5. Javascript parseInt()和parseFloat()的用法

    parseInt()方法首先查看位置0处的 字符,判断它是否是个有效数字:如果不是,该方法将返回NaN,不再继续执行其他操作.但如果该字符是有效数字,该方法将查看位置1处的字符,进行同样的 测试.这一 ...

  6. 导入project后lib文件夹一直没有jar包

    原因: 导入project,倒错了项目,只因为后面新建的项目名称有和前面的差不多,导致自己选择了原来的项目,因此,项目的注解一直报错.

  7. JAVA基础语法动手动脑实践体会

    一.运行EnumTest.java程序. public class EnumTest { public static void main(String[] args) { Size s=Size.SM ...

  8. java获取Excel的导入

    先准备好这2个架包 import java.io.*; import org.apache.commons.io.FileUtils; import org.apache.poi.hssf.userm ...

  9. javaScript实现轮播图

    一.需求分析 在首页完成对轮播图的效果实现,完成自动切换图片的功能. 二.技术分析 获取元素 document.getElementById(“id 名称”) 事件(onload) 定时操作: set ...

  10. vim尝试

    http://3502990.blog.51cto.com/3492990/985750