Don't know why my python solution TLE


  • 0
    J
    def threeSum(self, nums):
        d=dict()
        n=len(nums)
        ans=[]
        for num in nums:
            if num in d:
                d[num]+=1
            else:
                d[num]=1
        for i in range(n-1):
            for j in range(i+1,n):
                if -(nums[i]+nums[j]) in d:
                    temp=sorted([nums[i],nums[j],-nums[i]-nums[j]])
                    if temp in ans:
                        continue
                    if nums[i]==nums[j]==0:
                        if d[0]!=3:
                            continue
                    if nums[i]==nums[j] and nums[j]!=0:
                        if d[nums[i]]!=2:
                            continue
                    if -nums[i]-nums[j]==nums[j] and nums[j]!=0:
                        if d[nums[j]]!=2:
                            continue
                    ans.append(temp)
        return ans

  • 0
    Z

    This operation is not O(1) since ans is a list rather than dict

    if temp in ans:
    

Log in to reply
 

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