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))

题解:

1、自己想得思路:构建一个list,然后比较各数的大小,将其插入到合适的位置

class Solution:
    # @param {integer[]} nums1
    # @param {integer[]} nums2
    # @return {float}
    def findMedianSortedArrays(self, nums1, nums2):
        nums = nums1[:]    # 构建一个list,并将nums1的值赋给它
        x = len(nums1)
        y = len(nums2)
        for i in range(x + y):
            if i < len(nums):
                if len(nums2) == 0: break    # 如果nums2没有数了就跳出
                elif nums[i] < nums2[0]:
                    continue
                else:    # 否则将nums2[0]插入到i位置
                    num = nums2.pop(0)
                    nums.insert(i, num)
            else: break
        nums.extend(nums2)
        n = len(nums)/2        # 输出结果
        if len(nums)%2 == 0: return (nums[n] + nums[n-1])/2.0
        else: return nums[n]

2、参考网上的解题思路(http://c4fun.cn/blog/2014/03/20/leetcode-solution-02/)

用求两个有序数组的第K大数的方法:

假设A数组中取第X个数, B数组中取第Y个数,并且满足X+Y=K, 若A[X] < B[Y],则比A[X]小的数必然少于K个,也就是说A[1]到A[X]都比第K个数要小,可以舍弃掉然后求第K-X小的数,反之亦然

class Solution:
    def findMedianSortedArrays(self, A, B):
        totlen = len(A) + len(B)
        if (1 & totlen):    # 通过位运算判断奇偶数,nice
            return self.findK(A, B, (totlen+1)/2)
        else:
            return (self,findK(A, B, totlen/2) + self.findK(A, B, totlen/2+1))/2.0

    def findK(self, A, B, K):
        la, lb, pa, pb = len(A), len(B), min(K/2, len(A)), K - (min(K/2, len(A)))
        if (la > lb): return self.findK(B, A, K)
        if (la == 0): return B[K-1]
        if (K == 1): return min(A[0], B[0])
        if A[pa-1] < B[pb-1]: return self.findK(A[pa:], B, K-pa)
        elif A[pa-1] > B[pb-1]: return self.findK(A, B[pb:], K-pb)
        else: return A[pa-1]
时间: 2024-11-05 13:28:28

Leetcode 解题 Median of Two sorted arrays的相关文章

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 OJ - 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)). 解题思路: 将原问题转变成一个寻找第k小数的问题(假设两个原序列升序排列),这样中位数实际上是第(m+n)/2小的数.所以只要解决了第k小数的问题,原问题也得以解决

【题解】【数组】【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

leetcode 2.Median of Two Sorted Arrays

Median of Two Sorted Arrays Problem: 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][JavaScript]Median of Two Sorted Arrays

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)). https://leetcode.com/problems/median-of-two-sorted

第三周 leetcode 4. Median of Two Sorted Arrays (HARD)

4. Median of Two Sorted Arrays 给定两个有序的整数序列.求中位数,要求复杂度为对数级别. 通常的思路,我们二分搜索中位数,对某个序列里的某个数 我们可以在对数时间内通过二分算法求得两个序列中比它小的数,整体复杂度也是对数级别.但是代码实现较为困难. 换一个思路,我们把中位数不要当作一个数,而当作一个序列的划分.划分后,序列的左半部设为L,右半部设为R 满足max(L)<=min(R)且满足len(L)==len(R) 二分搜索这个划分即可.对于A+B的长度为奇数的情

【LeetCode】Median of Two Sorted Arrays (2 solutions)

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)). 解法一:保底做法,O(m+n)复杂度 按照归并排序的思路,数到median,然后计算返回. 需要注意: 如果是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)). 首先假设数组A和B的元素个数都大于k/2,我们比较A[k/2-1]和B[k/2-1]两个元素,这两个元素分别表示A的

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)). 解题思路: 本题要求求解的是两个有序序列的中位数.本质上就是求两个有序序列“第k小的数“的变形.假设两个有序序列一个长为m,另一个长为n,则我们