Why my code catch the TypeError: unsupported operand type(s) for +: 'int' and 'NoneType'


  • 0
    def findMedianSortedArrays(self, nums1, nums2):
        len1=len(nums1)
        len2=len(nums2)
        
        if not (len1+len2)%2:
            m1=self.findkth(nums1,len1,nums2,len2,(len1+len2)/2)
            m2=self.findkth(nums1,len1,nums2,len2,(len1+len2)/2+1)
            #m2 is None type on the condition m==0,while a[k-1] gets valid value.
            return (m1+m2)/2.0
        else:
            return self.findkth(nums1,len1,nums2,len2,(len1+len2)/2+1)
            
            
    def findkth(self,a,n,b,m,k):
            print a,n,b,m,k
            if n<=0:
                return b[k-1]#can't get the return value sometime
            if m<=0:
                return a[k-1] #can't get the return value sometime
            if k<=1:
                return min(a[0],b[0])
            if (n/2+m/2+1)>=k:
                if a[n/2]>=b[m/2]:
                    self.findkth(a,n/2,b,m,k)
                else:
                    self.findkth(a,n,b,m/2,k)
            else:
                if a[n/2]>=b[m/2]:
                    self.findkth(a,n,b[m/2+1:],m-(m/2+1),k-(m/2+1))
                else:
                    self.findkth(a[n/2+1:],n-(n/2+1),b,m,k-(n/2+1))

  • 0
    P

    @Tanych said in Why my code catch the TypeError: unsupported operand type(s) for +: 'int' and 'NoneType':

        print a,n,b,m,k
    

    findkth doesn't return any value for some cases, where it recursively calls itself.


Log in to reply
 

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