Leetcode Note: Go - Count Square Sum Triples

Count Square Sum Triples - LeetCode
https://leetcode.com/problems/count-square-sum-triples/

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

回答

Golang (n*n) - Count Square Sum Triples - LeetCode
https://leetcode.com/problems/count-square-sum-triples/solutions/1333052/golang-n-n/

func countTriples(n int) int {
    m := map[int]int{}
    for i := 1; i <= n; i++ {
        m[i*i] = i
    }
    count := 0
    for i := 1; i <= n; i++ {
        for j := i; j <= n; j++ {
            if _, exists := m[i*i + j*j]; exists {
                count += 2
            }
        }
    }
    return count
}