Python语言,leetcode题库刷题记录 (四)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 sorted arrays. The overall run time complexity should be O(log (m+n)).

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
class Solution(object):
    def findMedianSortedArrays(self, nums1, nums2):
        """
        :type nums1: List[int]
        :type nums2: List[int]
        :rtype: float
        """
        while 1:
            if len(nums1)+len(nums2) <= 2:
                if len(nums1) == 0:
                    res = float(nums2[0]+nums2[-1])/2
                elif len(nums2) == 0:
                    res = float(nums1[0]+nums1[-1])/2
                else:
                    res = float(nums1[0]+nums2[0])/2
                break;
            if len(nums1) == 0:
                if len(nums2) > 2:
                    del nums2[0]
                    del nums2[-1]
                elif len(nums2) > 1:
                    res = float(nums2[0]+nums2[-1])/2
                    break
                else:
                    res = nums2[0]
                    break;
            elif len(nums2) == 0:
                if len(nums1) > 2:
                    del nums1[0]
                    del nums1[-1]
                elif len(nums1) > 1:
                    res = float(nums1[0]+nums1[-1])/2
                    break;
                else:
                    res = nums1[0]
                    break;
            else:
                if nums1[0] < nums2[0]:
                    del nums1[0]
                elif nums1[0] > nums2[0]:
                    del nums2[0]
                else:
                    if len(nums1) > len(nums2):
                        del nums1[0]
                    else:
                        del nums2[0]

                if len(nums1) == 0:
                    del nums2[-1]
                elif len(nums2) == 0:
                    del nums1[-1]
                else:
                    if nums1[-1] > nums2[-1]:
                        del nums1[-1]
                    elif nums1[-1] < nums2[-1]:
                        del nums2[-1]
                    else:
                        if len(nums1) > len(nums2):
                            del nums1[-1]
                        else:
                            del nums2[-1]
        return float(res)

思想是每次循环删除两个list中一个最小的和一个最大的,最后剩下的一个数或者两个数的均值就是要返回的值。

但是要判断的情况太多了,代码写的又臭又长,看了一下评论区也没有特别赏心悦目的,就这样吧。

感觉下一题没脸发出来了……

时间: 2024-10-19 15:53:46

Python语言,leetcode题库刷题记录 (四)Median of Two Sorted Arrays的相关文章

LeetCode 第二题,Median of Two Sorted Arrays

题目再现 There are two sorted arrays A and B 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)). 题意解析 题目意思是给两个大小为m,n的有序数组(m,n可能为0),要求找出这两个数组的中位数.并且程序的时间复杂度必须不能超过O(log(m+n)). 这道题的

周刷题第二期总结(Longest Substring Without Repeating Characters and Median of Two Sorted Arrays)

这周前面刷题倒是蛮开心,后面出了很多别的事情和问题就去忙其他的,结果又只完成了最低目标. Lonest Substring Without Repeating Characters: Given a string, find the length of the longest substring without repeating characters. Examples: Given "abcabcbb", the answer is "abc", which t

[leetcode]Median of Two Sorted Arrays @ Python

原题地址:https://oj.leetcode.com/problems/median-of-two-sorted-arrays/ 题意:There are two sorted arrays A and B 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)). 解题思路:这道题要求两个已经排好

LeetCode(3) || Median of Two Sorted Arrays

LeetCode(3) || Median of Two Sorted Arrays 题记 之前做了3题,感觉难度一般,没想到突然来了这道比较难的,星期六花了一天的时间才做完,可见以前基础太差了. 题目内容 There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should

LeetCode【4】. Median of Two Sorted Arrays --java的不同方法实现

Median of Two Sorted Arrays 这道题确实有点难,想挺久,看别人答案也是不能一下子就明白.题目难度为Hard,原题如下: 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)). 给定两个已

[LeetCode] Median of Two Sorted Arrays [16]

题目 There are two sorted arrays A and B 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)). 原题链接(点我) 解题思路 返回两个排序数组的中位数.这个题可以有以下几个思路: 首先可以想到的是将两个数组merge起来,然后返回其中位数. 第二个是,类似merg

leetcode | Median of Two Sorted Arrays 寻找2个有序数组中第k大的值

问题 Median of Two Sorted Arrays There are two sorted arrays A and B 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)). 分析 本题更经典通用的描述方式时: 给定2个有序数组,找出2个数组中所有元素中第k大的元素. 思路1 直观思

LeetCode——Median of Two Sorted Arrays

There are two sorted arrays A and B 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)). 有两个已排序的数组A和B,大小为m 和 n. 找出两数组的中位数 时间复杂度应该为 O(log (m+n)). 简单粗暴的方法就是把两个数组合并成一个数组,排序,取中位数.

【题解】【数组】【Leetcode】Median of Two Sorted Arrays

Median of Two Sorted Arrays There are two sorted arrays A and B 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)). ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22