Leetcode题解(Python): 4.Median of Two Sorted Arrays (非递归)

来源:互联网 发布:papi 知乎 编辑:程序博客网 时间:2024/06/04 18:43

【题目】
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)).

【题解】
刚开始,因为写博客的原因看了眼前几道题的难度系数,这道题是5,当时以为很难,但是上手后感觉并不是很难。主要是借鉴了本科数据结构里学习的“将两个有序链表合并为一个有序链表”的思路。
代码:

class Solution(object):    def findMedianSortedArrays(self, nums1, nums2):        """        :type nums1: List[int]        :type nums2: List[int]        :rtype: float        """        m = len(nums1);        n = len(nums2);        isEven = True;//用于记录列表总长是不是偶数        mid = 0;//用于记录列表中位数的下标        if (m + n) % 2 == 0:            isEven = True;            mid = (m + n) / 2;            if mid > 0:                mid -= 1;        else:            isEven = False;            mid = (m + n - 1) / 2;        i = j = 0;        result = 0;        needContinue = True;        tmp1 = tmp2 = 0;        //两个列表都有未遍历的元素        while i < m and j < n:            if nums1[i] <= nums2[j]:                tmp1 = nums1[i];                if i + j == mid:                    if isEven:                        i += 1;                        if i < m and nums1[i] <= nums2[j]:                            tmp2 = nums1[i];                        else:                            tmp2 = nums2[j];                        result = 1.0 * (tmp1 + tmp2) / 2;                    else:                        result = tmp1;                    needContinue = False;                    break;                i += 1;            else:                tmp1 = nums2[j];                if i + j == mid:                    if isEven:                        j += 1;                        if j < n and nums2[j] <= nums1[i]:                            tmp2 = nums2[j];                        else:                            tmp2 = nums1[i];                        result = 1.0 * (tmp1 + tmp2) / 2;                    else:                        result = tmp1;                    needContinue = False;                    break;                j += 1;        if needContinue:            //nums2中元素已经遍历完,但仍没有找到中位数,继续遍历nums1            while i < m:                if i + j == mid:                    if isEven:                        if i + 1 < m:                            result = 1.0 * (nums1[i] + nums1[i + 1]) / 2;                        else:                            result = nums1[i];                    else:                        result = nums1[i];                    needContinue = False;                    break;                i += 1;            if needContinue:                //nums1遍历完,仍未找到中位数,继续遍历nums2                while j < n:                    if i + j == mid :                        if isEven:                            if j + 1 < n:                                result = 1.0 * (nums2[j] + nums2[j + 1]) / 2;                            else:                                result = nums2[j];                        else:                            result = nums2[j];                        break;                    j += 1;        return result;

这种方法在leetcode上的执行时间是120ms,击败82%+。 最坏时间复杂度为O(m+n)。就代码的简洁来说,感觉应该有更好的方法,在网上找了找,貌似都是用递归去写的。在这里就不再赘述,有兴趣的朋友可以自己百度~

水平有限,欢迎指正~
如需转发,请注明出处,thx~

0 0
原创粉丝点击