zoukankan      html  css  js  c++  java
  • 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));
    	}
    }
    
  • 相关阅读:
    八卦——朋友的老公有外遇
    吃,玩——幸福的上海一天
    婚礼——金茂群楼豪华婚礼
    吃狂吃大喜九
    玩——苏州粗体验
    XCF之原形
    快速类型判定
    ReaderWriterLockSlim使用注意事项
    WCF服务端基于配置的实现——路由
    Opera使用心得
  • 原文地址:https://www.cnblogs.com/denggelin/p/11971761.html
Copyright © 2011-2022 走看看