classDetectSquares:def__init__(self):self.cnt=defaultdict(Counter)defadd(self,point:List[int])->None:x,y=pointself.cnt[x][y]+=1defcount(self,point:List[int])->int:x1,y1=pointifx1notinself.cnt:return0ans=0forx2inself.cnt.keys():ifx2!=x1:d=x2-x1ans+=self.cnt[x2][y1]*self.cnt[x1][y1+d]*self.cnt[x2][y1+d]ans+=self.cnt[x2][y1]*self.cnt[x1][y1-d]*self.cnt[x2][y1-d]returnans# Your DetectSquares object will be instantiated and called as such:# obj = DetectSquares()# obj.add(point)# param_2 = obj.count(point)
classDetectSquares{privateMap<Integer,Map<Integer,Integer>>cnt=newHashMap<>();publicDetectSquares(){}publicvoidadd(int[]point){intx=point[0],y=point[1];cnt.computeIfAbsent(x,k->newHashMap<>()).merge(y,1,Integer::sum);}publicintcount(int[]point){intx1=point[0],y1=point[1];if(!cnt.containsKey(x1)){return0;}intans=0;for(vare:cnt.entrySet()){intx2=e.getKey();if(x2!=x1){intd=x2-x1;varcnt1=cnt.get(x1);varcnt2=e.getValue();ans+=cnt2.getOrDefault(y1,0)*cnt1.getOrDefault(y1+d,0)*cnt2.getOrDefault(y1+d,0);ans+=cnt2.getOrDefault(y1,0)*cnt1.getOrDefault(y1-d,0)*cnt2.getOrDefault(y1-d,0);}}returnans;}}/** * Your DetectSquares object will be instantiated and called as such: * DetectSquares obj = new DetectSquares(); * obj.add(point); * int param_2 = obj.count(point); */
classDetectSquares{public:DetectSquares(){}voidadd(vector<int>point){intx=point[0],y=point[1];++cnt[x][y];}intcount(vector<int>point){intx1=point[0],y1=point[1];if(!cnt.count(x1)){return0;}intans=0;for(auto&[x2,cnt2]:cnt){if(x2!=x1){intd=x2-x1;auto&cnt1=cnt[x1];ans+=cnt2[y1]*cnt1[y1+d]*cnt2[y1+d];ans+=cnt2[y1]*cnt1[y1-d]*cnt2[y1-d];}}returnans;}private:unordered_map<int,unordered_map<int,int>>cnt;};/** * Your DetectSquares object will be instantiated and called as such: * DetectSquares* obj = new DetectSquares(); * obj->add(point); * int param_2 = obj->count(point); */
typeDetectSquaresstruct{cntmap[int]map[int]int}funcConstructor()DetectSquares{returnDetectSquares{map[int]map[int]int{}}}func(this*DetectSquares)Add(point[]int){x,y:=point[0],point[1]if_,ok:=this.cnt[x];!ok{this.cnt[x]=map[int]int{}}this.cnt[x][y]++}func(this*DetectSquares)Count(point[]int)(ansint){x1,y1:=point[0],point[1]ifcnt1,ok:=this.cnt[x1];ok{forx2,cnt2:=rangethis.cnt{ifx2!=x1{d:=x2-x1ans+=cnt2[y1]*cnt1[y1+d]*cnt2[y1+d]ans+=cnt2[y1]*cnt1[y1-d]*cnt2[y1-d]}}}return}/** * Your DetectSquares object will be instantiated and called as such: * obj := Constructor(); * obj.Add(point); * param_2 := obj.Count(point); */