diff --git a/src/leetcode/00004.median-of-two-sorted-arrays/4.md b/src/leetcode/00004.median-of-two-sorted-arrays/4.md index d254a8a..86e998d 100644 --- a/src/leetcode/00004.median-of-two-sorted-arrays/4.md +++ b/src/leetcode/00004.median-of-two-sorted-arrays/4.md @@ -1 +1,10 @@ # [4. 寻找两个正序数组的中位数](https://leetcode.cn/problems/median-of-two-sorted-arrays/) + +## 中位数 + +```python +def mean(nums): + nums.sort() + mid = len(nums) // 2 + return (nums[mid] + nums[~mid]) / 2 +``` diff --git a/src/leetcode/00004.median-of-two-sorted-arrays/4.py b/src/leetcode/00004.median-of-two-sorted-arrays/4.py index cde2a2b..3ad144c 100644 --- a/src/leetcode/00004.median-of-two-sorted-arrays/4.py +++ b/src/leetcode/00004.median-of-two-sorted-arrays/4.py @@ -1,29 +1,42 @@ from typing import List +# class Solution: +# def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float: +# res = nums1[::] + +# def inset(t): +# left = 0 +# right = len(res) - 1 +# while left <= right: +# mid = (left + right) >> 1 +# if res[mid] > t: +# right = mid - 1 +# else: +# left = mid + 1 +# res.insert(left, t) + +# for i in nums2: +# inset(i) + + +# length = len(res) +# if length % 2 == 0: +# return (res[length // 2 - 1] + res[length // 2]) / 2 +# else: +# return res[length // 2] class Solution: def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float: - res = nums1[::] - - def inset(t): - left = 0 - right = len(res) - 1 - while left <= right: - mid = (left + right) >> 1 - if res[mid] > t: - right = mid - 1 - else: - left = mid + 1 - res.insert(left, t) - - for i in nums2: - inset(i) - - length = len(res) - if length % 2 == 0: - return (res[length // 2 - 1] + res[length // 2]) / 2 - else: - return res[length // 2] + res = [] + while nums1 and nums2: + if nums1[0] >= nums2[0]: + res.append(nums2.pop(0)) + else: + res.append(nums1.pop(0)) + res += nums1 + nums2 + + mid = len(res) // 2 + return (res[mid] + res[~mid]) / 2 if __name__ == "__main__":