Created
October 8, 2024 03:55
-
-
Save woshichuanqilz/1e52e57bbae5079044fb9a68167f8c75 to your computer and use it in GitHub Desktop.
LC4. 最简单的双指针的解法
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution: | |
def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float: | |
l1 = len(nums1) | |
l2 = len(nums2) | |
total_len = 0 | |
index1 = 0 | |
index2 = 0 | |
last1 = 0 | |
last2 = 0 | |
while True: | |
last2 = last1 | |
if index1 == l1: | |
last1 = nums2[index2] | |
index2 += 1 | |
elif index2 == l2: | |
last1 = nums1[index1] | |
index1 += 1 | |
elif nums1[index1] <= nums2[index2]: | |
last1 = nums1[index1] | |
index1 += 1 | |
elif nums1[index1] > nums2[index2]: | |
last1 = nums2[index2] | |
index2 += 1 | |
else: | |
print('error') | |
total_len += 1 | |
if total_len == (l1 + l2)//2 + 1: | |
break | |
if (l1 + l2) % 2 == 0: | |
return (last1 + last2)/2 | |
else: | |
return last1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment