Intuitive Python solution - iterative


  • 0
    T
    class Solution(object):
        def findMedianSortedArrays(self, nums1, nums2):
            """
            :type nums1: List[int]
            :type nums2: List[int]
            :rtype: float
            """        
            target = len(nums1+nums2)/2+0.5
            split = (target%1==0.5)
            target = int(target)
            current = -999 #doesnt matter
            for i in range(target+1):
                previous = current
                if not nums1 and not nums2:
                    break
                if not nums1:
                    current = nums2.pop(0)
                elif not nums2:
                    current = nums1.pop(0)
                elif nums1[0]<nums2[0]:
                    current = nums1.pop(0)
                else:
                    current = nums2.pop(0)
            if split:
                previous = (current+previous)/2
            return previous
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.