2013.检测正方形
2013.检测正方形
题解
用map存边,形参给出一个点,遍历map,又确定一个点,由此就可以推出是不是正方形了
代码
package main type DetectSquares struct { mp map[int]map[int]int } func Constructor() DetectSquares { return DetectSquares{mp: make(map[int]map[int]int)} } func (this *DetectSquares) Add(point []int) { x := point[0] y := point[1] if this.mp[y] == nil { this.mp[y] = make(map[int]int) } this.mp[y][x]++ } func (this *DetectSquares) Count(point []int) int { x := point[0] y := point[1] result := 0 for rangeY, mapX := range this.mp { if rangeY != y { sideLen := y - rangeY result += mapX[x] * this.mp[y][x-sideLen] * mapX[x-sideLen] result += mapX[x] * this.mp[y][x+sideLen] * mapX[x+sideLen] } } return result }