《LeetCode-0004》 寻找两个有序数组的中位数-Median of Two Sorted Arrays
题目
给定两个大小为 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的更多相关文章
- 力扣 -- 寻找两个有序数组的中位数 Median of Two Sorted Arrays python实现
题目描述: 中文: 给定两个大小为 m 和 n 的有序数组 nums1 和 nums2. 请你找出这两个有序数组的中位数,并且要求算法的时间复杂度为 O(log(m + n)). 你可以假设 nums ...
- Java实现 LeetCode 4 寻找两个有序数组的中位数
寻找两个有序数组的中位数 给定两个大小为 m 和 n 的有序数组 nums1 和 nums2. 请你找出这两个有序数组的中位数,并且要求算法的时间复杂度为 O(log(m + n)). 你可以假设 n ...
- 0004. 寻找两个有序数组的中位数(Java)
4. 寻找两个有序数组的中位数 https://leetcode-cn.com/problems/median-of-two-sorted-arrays/ 最简单的就是用最简单的,把两个数组分别抽出然 ...
- 【LeetCode】寻找两个有序数组的中位数【性质分析+二分】
给定两个大小为 m 和 n 的有序数组 nums1 和 nums2. 请你找出这两个有序数组的中位数,并且要求算法的时间复杂度为 O(log(m + n)). 你可以假设 nums1 和 nums2 ...
- [LeetCode] 4. 寻找两个有序数组的中位数
题目链接:https://leetcode-cn.com/problems/median-of-two-sorted-arrays/ 题目描述: 给定两个大小为 m 和 n 的有序数组 nums1 和 ...
- 【LeetCode】寻找两个有序数组的中位数
给定两个大小为 m 和 n 的有序数组 nums1 和 nums2. 请你找出这两个有序数组的中位数,并且要求算法的时间复杂度为 O(log(m + n)). 你可以假设 nums1 和 nums2 ...
- leetcode 4 寻找两个有序数组的中位数 二分法&INT_MAX
小知识 INT_MIN在标准头文件limits.h中定义. #define INT_MAX 2147483647#define INT_MIN (-INT_MAX - 1) 题解思路 其实是类似的二分 ...
- [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 ...
- leetcode 4寻找两个有序数组的中位数
最优解O(log(min(m,n))) /** 之前用合并有序数组的思想做了O((m+n+1)/2),现在试一试O(log(min(m,n))) 基本思路为:通过二分查找较小的数组得到对应的中位数(假 ...
随机推荐
- 插件化开发—动态载入技术载入已安装和未安装的apk
首先引入一个概念,动态载入技术是什么?为什么要引入动态载入?它有什么优点呢?首先要明确这几个问题.我们先从 应用程序入手,大家都知道在Android App中.一个应用程序dex文件的方法数最大不能超 ...
- BEGINNING SHAREPOINT® 2013 DEVELOPMENT 第12章节--SP 2013中远程Event Receivers
BEGINNING SHAREPOINT® 2013 DEVELOPMENT 第12章节--SP 2013中远程Event Receivers 本章中,你讲学到: 了解远程evernt ...
- oc const 关键字 对指针的理解
/* int const *p; *p是常量, p是变量 const int *p; *p是常量, p是变量 int * const p; *p是变量, p是常量 const int * const ...
- Angular常用标记
(如果没有特别指明,则所有的HTML元素都支持该标记) (如果没有特别指明,则 AngularJS 指令不会覆盖原生js的指令) 1.数据绑定类: 1.插值语法:{{}} 2.标签内容绑定:ng-bi ...
- UVA - 10324 Zeros and Ones
Description Given a string of 0's and 1's up to 1000000 characters long and indices i and j, you are ...
- Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/poi/util/POILogFactory
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/poi/util/POILogFacto ...
- bzoj4403 序列统计——组合数学
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4403 一开始想了个 O(n) 的做法,不行啊... O(n)想法是这样的:先考虑递推,设 f ...
- 转载:C语言的字节对齐及#pragma pack的使用
C语言的字节对齐及#pragma pack的使用 C编译器的缺省字节对齐方式(自然对界) 在缺省情况下,C编译器为每一个变量或是数据单元按其自然对界条件分配空间. 在结构中,编译器为结构的每个成员 ...
- oracleXE简易版---使用基础
1.开启服务 2.更改端口号 a) EX修改HTTP服务端口,避免和TOMCAT端口冲突 Oracel默认会启动HTTP服务,占有端口8080,但一般8080时TOMCAT的配置端口 可以修改TO ...
- 洛谷P1726 上白泽慧音(Tarjan强连通分量)
P1726 上白泽慧音 题目描述 在幻想乡,上白泽慧音是以知识渊博闻名的老师.春雪异变导致人间之里的很多道路都被大雪堵塞,使有的学生不能顺利地到达慧音所在的村庄.因此慧音决定换一个能够聚集最多人数的村 ...