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. 进程与进程之间通信Manager

    #!/usr/bin/env python from multiprocessing import Process,Manager #Manager进程与进程之间通信 def Foo(i,dic): ...

  2. Linux共享对象之编译参数 -fPIC

    转载自:https://www.cnblogs.com/cswuyg/p/3830703.html     在Linux系统中,动态链接文件称为动态共享对象(DSO,Dynamic Shared Ob ...

  3. PCL—关键点检测(Harris)低层次点云处理

    博客转载自:http://www.cnblogs.com/ironstark/p/5064848.html 除去NARF这种和特征检测联系比较紧密的方法外,一般来说特征检测都会对曲率变化比较剧烈的点更 ...

  4. Xcode 运行时找不到xib资源文件

    调试运行时候,提示找不到xib(或者其他)资源文件,在工程中确实看的到该资源文件,到具体运行的资源目录([[NSBundlemainBundle] resourcePath]),没有看到该文件,而其他 ...

  5. 远程桌面--------ms12-020 漏洞复现 (死亡蓝屏)

    漏洞名:MS12-020(全称:Microsoft windows远程桌面协议RDP远程代码执行漏洞) 介绍:RDP协议是一个多通道的协议,让用户连上提供微软终端机服务的电脑. windows在处理某 ...

  6. 存储类型auto,static,extern,register的区别 <转>

    变量和函数的属性包括数据类型和数据的存储类别,存储类别指数据在内存中存储方式(静态和动态),包含auto,static,register,extern四种. 内存中.具体点来说内存分为三块:静态区,堆 ...

  7. POJ 1795 DNA Laboratory (贪心+状压DP)

    题意:给定 n 个 字符串,让你构造出一个最短,字典序最小的字符串,包括这 n 个字符串. 析:首先使用状压DP,是很容易看出来的,dp[s][i] 表示已经满足 s 集合的字符串以 第 i 个字符串 ...

  8. ConfigParser简介

    一.ConfigParser简介 ConfigParser 是用来读取配置文件的包.配置文件的格式如下:中括号“[ ]”内包含的为section.section 下面为类似于key-value 的配置 ...

  9. dojo1.7 加载器

    原文地址:http://dojotoolkit.org/documentation/tutorials/1.7/modules/ dojo现在支持在异步模块异步(AMD)定义中加入模块写入功能了,这使 ...

  10. C# 顺序表---增删改查--逆至--删除最小值

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...