其他分享
首页 > 其他分享> > LeetCode_447. Number of Boomerangs

LeetCode_447. Number of Boomerangs

作者:互联网

 

447. Number of Boomerangs

Easy

Given n points in the plane that are all pairwise distinct, a "boomerang" is a tuple of points (i, j, k) such that the distance between i and j equals the distance between i and k (the order of the tuple matters).

Find the number of boomerangs. You may assume that n will be at most 500 and coordinates of points are all in the range [-10000, 10000] (inclusive).

Example:

Input:
[[0,0],[1,0],[2,0]]

Output:
2

Explanation:
The two boomerangs are [[1,0],[0,0],[2,0]] and [[1,0],[2,0],[0,0]]

 

package leetcode.easy;

public class NumberOfBoomerangs {
	public int numberOfBoomerangs(int[][] points) {
		int res = 0;
		java.util.HashMap<Integer, Integer> map = new java.util.HashMap<Integer, Integer>();
		for (int[] i : points) {
			for (int[] j : points) {
				if (i == j) {
					continue;
				}
				Integer dist = (i[0] - j[0]) * (i[0] - j[0]) + (i[1] - j[1]) * (i[1] - j[1]);
				Integer prev = map.get(dist);
				if (prev != null) {
					res += 2 * prev;
				}
				map.put(dist, (prev == null) ? 1 : prev + 1);
			}
			map.clear();
		}
		return res;
	}

	@org.junit.Test
	public void test() {
		int[][] points = { { 0, 0 }, { 1, 0 }, { 2, 0 } };
		System.out.println(numberOfBoomerangs(points));
	}
}

 

标签:map,int,Boomerangs,447,public,points,res,prev,LeetCode
来源: https://www.cnblogs.com/denggelin/p/11971761.html