python O(n^2) solution with hashtable


  • 2
    E
    class Solution(object):
        def fourSumCount(self, A, B, C, D):
            """
            :type A: List[int]
            :type B: List[int]
            :type C: List[int]
            :type D: List[int]
            :rtype: int
            """
            hashtable = {}
            for a in A:
                for b in B :
                    if a + b in hashtable :
                        hashtable[a+b] += 1
                    else :
                        hashtable[a+b] = 1
            count = 0         
            for c in C :
                for d in D :
                    if -c - d in hashtable :
                        count += hashtable[-c-d]
            return count

  • 0
    H

    Very nice, thanks for sharing!


  • 0
    T

Log in to reply
 

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