题目
给定两个大小为 m 和 n 的有序数组nums1和 nums2。

请你找出这两个有序数组的中位数,并且要求算法的时间复杂度为 O(log(m + n))。

你可以假设 nums1 和 nums2 不会同时为空。

示例 1:

nums1 = [1, 3]
nums2 = [2]

则中位数是 2.0
1
2
3
4
示例 2:

nums1 = [1, 2]
nums2 = [3, 4]

则中位数是 (2 + 3)/2 = 2.5
1
2
3
4
概念
中位数的概念:
对于有限的数集,可以通过把所有观察值高低排序后找出正中间的一个作为中位数。如果观察值有偶数个,通常取最中间的两个数值的平均数作为中位数。

复杂度 log(m+n),参考文章:
https://github.com/xitu/gold-miner/blob/master/TODO/what-does-the-time-complexity-o-log-n-actually-mean.md

思路
两个有序数组
中位数
复杂度O(log(m + n))
解题
第一次提交
臭不要脸的提交。本着试一试的态度提交。这个答案显然不符合负责度的要求。竟然是通过。

class Solution {
public double findMedianSortedArrays(int[] nums1, int[] nums2) {
List<Integer> nums_a = new ArrayList<>();
List<Integer> nums_b = new ArrayList<>(http://www.my516.com);
for (int i = 0; i < nums1.length; i++) {
nums_a.add(nums1[i]);
}
for (int i = 0; i < nums2.length; i++) {
nums_b.add(nums2[i]);
}
nums_a.addAll(nums_b);
Collections.sort(nums_a);
if (nums_a.size() % 2 == 0) {
int mid = nums_a.size() / 2;
return (nums_a.get(mid) + nums_a.get(mid - 1)) / 2.0f;
} else {
int mid = (nums_a.size() - 1) / 2;
return nums_a.get(mid);
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
第二次提交
关于这段代码,看详细题解,得多看几遍。
https://leetcode-cn.com/problems/median-of-two-sorted-arrays/solution/4-xun-zhao-liang-ge-you-xu-shu-zu-de-zhong-wei-shu/

class Solution {
public double findMedianSortedArrays(int[] nums1, int[] nums2) {
int n = nums1.length;
int m = nums2.length;
if (n > m) {
return findMedianSortedArrays(nums2, nums1);
}
int L1 = 0, L2 = 0, R1 = 0, R2 = 0, c1, c2, lo = 0, hi = 2 * n;
while (lo <= hi)
{
c1 = (lo + hi) / 2;
c2 = m + n - c1;
L1 = (c1 == 0) ? Integer.MIN_VALUE : nums1[(c1 - 1) / 2]; //map to original element
R1 = (c1 == 2 * n) ? Integer.MAX_VALUE : nums1[c1 / 2];
L2 = (c2 == 0) ? Integer.MIN_VALUE : nums2[(c2 - 1) / 2];
R2 = (c2 == 2 * m) ? Integer.MAX_VALUE : nums2[c2 / 2];

if (L1 > R2) {
hi = c1 - 1;
} else if (L2 > R1) {
lo = c1 + 1;
} else {
break;
}
}
return (Math.max(L1, L2) + Math.min(R1, R2)) / 2.0;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
总结
思考未果可看题解,前提是必须先思考
概念的深入理解很重要
---------------------

《LeetCode-0004》 寻找两个有序数组的中位数-Median of Two Sorted Arrays的更多相关文章

  1. 力扣 -- 寻找两个有序数组的中位数 Median of Two Sorted Arrays python实现

    题目描述: 中文: 给定两个大小为 m 和 n 的有序数组 nums1 和 nums2. 请你找出这两个有序数组的中位数,并且要求算法的时间复杂度为 O(log(m + n)). 你可以假设 nums ...

  2. Java实现 LeetCode 4 寻找两个有序数组的中位数

    寻找两个有序数组的中位数 给定两个大小为 m 和 n 的有序数组 nums1 和 nums2. 请你找出这两个有序数组的中位数,并且要求算法的时间复杂度为 O(log(m + n)). 你可以假设 n ...

  3. 0004. 寻找两个有序数组的中位数(Java)

    4. 寻找两个有序数组的中位数 https://leetcode-cn.com/problems/median-of-two-sorted-arrays/ 最简单的就是用最简单的,把两个数组分别抽出然 ...

  4. 【LeetCode】寻找两个有序数组的中位数【性质分析+二分】

    给定两个大小为 m 和 n 的有序数组 nums1 和 nums2. 请你找出这两个有序数组的中位数,并且要求算法的时间复杂度为 O(log(m + n)). 你可以假设 nums1 和 nums2 ...

  5. [LeetCode] 4. 寻找两个有序数组的中位数

    题目链接:https://leetcode-cn.com/problems/median-of-two-sorted-arrays/ 题目描述: 给定两个大小为 m 和 n 的有序数组 nums1 和 ...

  6. 【LeetCode】寻找两个有序数组的中位数

    给定两个大小为 m 和 n 的有序数组 nums1 和 nums2. 请你找出这两个有序数组的中位数,并且要求算法的时间复杂度为 O(log(m + n)). 你可以假设 nums1 和 nums2  ...

  7. leetcode 4 寻找两个有序数组的中位数 二分法&INT_MAX

    小知识 INT_MIN在标准头文件limits.h中定义. #define INT_MAX 2147483647#define INT_MIN (-INT_MAX - 1) 题解思路 其实是类似的二分 ...

  8. [Swift]LeetCode4. 两个排序数组的中位数 | 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 ...

  9. leetcode 4寻找两个有序数组的中位数

    最优解O(log(min(m,n))) /** 之前用合并有序数组的思想做了O((m+n+1)/2),现在试一试O(log(min(m,n))) 基本思路为:通过二分查找较小的数组得到对应的中位数(假 ...

随机推荐

  1. 喜欢玩warcraft的ltl

    喜欢玩warcraft的ltl 时间限制:2000 ms  |  内存限制:65535 KB 难度:3 描写叙述 ltl 很喜欢玩warcraft.由于warcraft十分讲究团队总体实力,而他自己如 ...

  2. PAT Broken Keyboard (20)

    题目描写叙述 On a broken keyboard, some of the keys are worn out. So when you type some sentences, the cha ...

  3. 创建SharePoint 2010 Timer Job

    好久没有写博客了. 近期在使用SharePoint 2010中Timer Job的功能,有了一点心得,分享一下. 我个人觉得SharePoint Timer Job和Windows Service或者 ...

  4. 小胖说事31------iOS 真机编译错误&quot;“XXX”的 iPod&quot; and run &quot;XXX&quot; again, or if &quot;XXX&quot; is still running

    在真机上測试时用一会就出现例如以下信息,且应用挂掉. Restore the connection to ""XXX"的 iPod" and run " ...

  5. _io.TextIOWrapper

    ''' SELECT * FROM Info_Roles WHERE Flag=1 LIMIT 2; select top y * from 表 where 主键 not in(select top ...

  6. ios ionic 装平台 笔记

    1.安装cnpm : npm install -g cnpm --registry=https://registry.npm.taobao.org 2.An error occurred when I ...

  7. HDU 1299Diophantus of Alexandria

    Diophantus of Alexandria Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java ...

  8. compileSdkVersion, minSdkVersion 和 targetSdkVersion的选择(copy)

    英文原文:Picking your compileSdkVersion, minSdkVersion, and targetSdkVersion 作者:Ian Lake,Google Android ...

  9. androd基础入门---1环境

    1.项目结构特性 2.模拟器设置 3.编译器的下载 直接点击运行即可

  10. ckeditor使用时,第一次可以显示,修改后显示不了的问题

    1.谷歌浏览器会留有缓存,除去缓存后,就可以更改ckeditor了.下面是解决方法: