Number of Boomerangs Solutions in Java
Number 447
Difficulty Easy
Acceptance 51.9%
Link LeetCode
Solutions
Java solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/number-of-boomerangs/description//// Author : liuyubobobo/// Time : 2017-11-15import java.util.HashMap;/// Using Hash Map/// Time Complexity: O(n^2)/// Space Complexity: O(n)public class Solution {public int numberOfBoomerangs(int[][] points) {int res = 0;for( int i = 0 ; i < points.length ; i ++ ){// record中存储 点i 到所有其他点的距离出现的频次HashMap<Integer, Integer> record = new HashMap<Integer, Integer>();for(int j = 0 ; j < points.length ; j ++)if(j != i){// 计算距离时不进行开根运算, 以保证精度int dis = dis(points[i], points[j]);if(record.containsKey(dis))record.put(dis, record.get(dis) + 1);elserecord.put(dis, 1);}for(Integer dis: record.keySet())res += record.get(dis) * (record.get(dis) - 1);}return res;}private int dis(int[] pa, int pb[]){return (pa[0] - pb[0]) * (pa[0] - pb[0]) +(pa[1] - pb[1]) * (pa[1] - pb[1]);}public static void main(String[] args) {int[][] points = {{0, 0}, {1, 0}, {2, 0}};System.out.println((new Solution()).numberOfBoomerangs(points));}}