Python N^2 with dictionary


  • 0
    class Solution(object):
        def fourSumCount(self, A, B, C, D):
            ans, dic = 0, {}
            for i in xrange(len(A)):
                for j in xrange(len(B)):
                    tmp = A[i] + B[j]
                    if tmp in dic:
                        dic[tmp] += 1
                    else:
                        dic[tmp] = 1
            for i in xrange(len(C)):
                for j in xrange(len(D)):
                    tmp = -(C[i] + D[j])
                    if tmp in dic:
                        ans += dic[tmp]
            return ans
    

  • 0
    W
    This post is deleted!

Log in to reply
 

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