Leetcode Note: Go - Number of Equivalent Domino Pairs

Number of Equivalent Domino Pairs - LeetCode
https://leetcode.com/problems/number-of-equivalent-domino-pairs/

  • Go 言語で取り組んだメモ

回答

Go without hashmap O(n) - LeetCode Discuss
https://leetcode.com/problems/number-of-equivalent-domino-pairs/discuss/2459029/Go-without-hashmap-O(n)

func numEquivDominoPairs(dominoes [][]int) int {
    res, cache := 0, make([][]int, 10)
    
    for i := 0; i < 10; i++ {
        cache[i] = make([]int, 10)
    }
    
    for i := 0; i < len(dominoes); i++ {
        begin, end := dominoes[i][0], dominoes[i][1]
        
        res += cache[begin][end]
        
        if begin != end {
            res += cache[end][begin]
        }
        
        
        cache[begin][end]++
    }
    
    return res
}