-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path4.median-of-two-sorted-arrays.py
83 lines (76 loc) · 2.16 KB
/
4.median-of-two-sorted-arrays.py
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
#
# @lc app=leetcode id=4 lang=python3
#
# [4] Median of Two Sorted Arrays
#
# https://leetcode.com/problems/median-of-two-sorted-arrays/description/
#
# algorithms
# Hard (25.84%)
# Total Accepted: 404.3K
# Total Submissions: 1.6M
# Testcase Example: '[1,3]\n[2]'
#
# 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)).
#
# You may assume nums1 and nums2 cannot be both empty.
#
# 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
#
# https://blog.csdn.net/chen_xinjia/article/details/69258706
#
class Solution:
def findMedianSortedArrays(self, nums1, nums2):
m, n = len(nums1), len(nums2)
if m > n:
nums1, nums2, m, n = nums2, nums1, n, m # 保持 n 始终大于 m
if n == 0:
return None
imin, imax, half_len = 0, m, int((m + n + 1) / 2)
while imin <= imax:
i = int((imin + imax) / 2)
j = half_len - i
# 确定 i j 两个值
if i < m and nums2[j - 1] > nums1[i]:
imin = i + 1
elif i > 0 and nums1[i - 1] > nums2[j]:
imax = i - 1
else:
# i 的值已经确定,现在找中间值
if i == 0: # 确定左边界情况
max_of_left = nums2[j - 1]
elif j == 0:
max_of_left = nums1[i - 1]
else:
max_of_left = max(nums1[i - 1], nums2[j - 1])
if (m + n) % 2 == 1: # 奇数的情况下
return max_of_left
if i == m: # 确定右边界情况
min_of_right = nums2[j]
elif j == n:
min_of_right = nums1[i]
else:
min_of_right = min(nums1[i], nums2[j])
return (max_of_left + min_of_right) / 2.0
if __name__ == '__main__':
nums1, nums2 = [1, 3], [3, 4]
s = Solution()
print(s.findMedianSortedArrays(nums1, nums2))