/
4.寻找两个有序数组的中位数.java
87 lines (81 loc) · 2.18 KB
/
4.寻找两个有序数组的中位数.java
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/*
* @lc app=leetcode.cn id=4 lang=java
*
* [4] 寻找两个有序数组的中位数
*
* https://leetcode-cn.com/problems/median-of-two-sorted-arrays/description/
*
* algorithms
* Hard (37.11%)
* Likes: 2329
* Dislikes: 0
* Total Accepted: 162K
* Total Submissions: 435.9K
* Testcase Example: '[1,3]\n[2]'
*
* 给定两个大小为 m 和 n 的有序数组 nums1 和 nums2。
*
* 请你找出这两个有序数组的中位数,并且要求算法的时间复杂度为 O(log(m + n))。
*
* 你可以假设 nums1 和 nums2 不会同时为空。
*
* 示例 1:
*
* nums1 = [1, 3]
* nums2 = [2]
*
* 则中位数是 2.0
*
*
* 示例 2:
*
* nums1 = [1, 2]
* nums2 = [3, 4]
*
* 则中位数是 (2 + 3)/2 = 2.5
*
*
*/
// @lc code=start
class Solution {
public double findMedianSortedArrays(int[] nums1, int[] nums2) {
int m = nums1.length;
int n = nums2.length;
if (m > n) {
return findMedianSortedArrays(nums2, nums1); // 保证 m <= n
}
int imin = 0;
int imax = m;
while (imin <= imax) {
int i = (imin + imax) / 2;
int j = (m + n + 1) / 2 - i; // 保证奇数时,中位数在左边
if (j != 0 && i != m && nums2[j - 1] > nums1[i]) {
imin = i + 1;
} else if (i != 0 && j != n && nums1[i - 1] > nums2[j]) {
imax = i - 1;
} else {
int maxLeft = 0;
if (i == 0) {
maxLeft = nums2[j - 1];
} else if (j == 0) {
maxLeft = nums1[i - 1];
} else {
maxLeft = Math.max(nums2[j - 1], nums1[i - 1]);
}
if ((m + n) % 2 == 1)
return maxLeft;
int minRight = 0;
if (j == n) {
minRight = nums1[i];
} else if (i == m) {
minRight = nums2[j];
} else {
minRight = Math.min(nums2[j], nums1[i]);
}
return (minRight + maxLeft) / 2.0;
}
}
return 0.0;
}
}
// @lc code=end