n^2 implementation in swift


  • 0
    M

    Thank @asurana28 's solution, I implemented in in swift 3.0

    class Solution {
        func fourSumCount(_ A: [Int], _ B: [Int], _ C: [Int], _ D: [Int]) -> Int {
            let length = A.count
            var dic:[Int:Int] = [:]
            for a in A {
                for b in B {
                    let t = a+b
                    if let v = dic[t] {
                        dic[t] = v+1
                    } else {
                        dic[t] = 1
                    }
                }
            }
            
            var res = 0
            for c in C {
                for d in D {
                    let t = c+d
                    if let v = dic[-t] {
                        res += v
                    }
                }
            }
            return res
        }
    }
    

Log in to reply
 

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