博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
447. Number of Boomerangs
阅读量:4602 次
发布时间:2019-06-09

本文共 1369 字,大约阅读时间需要 4 分钟。

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:2Explanation:The two boomerangs are [[1,0],[0,0],[2,0]] and [[1,0],[2,0],[0,0]]

分析:循环查找每个点, 每个点都用一个hashmap存放其他点到该点得值 作为key,i,j = i, k。有重复则value+1;

计算总共有几对是用组合 n * n-1;

public class Solution {    public int numberOfBoomerangs(int[][] points) {        int sum = 0;        if(points == null || points.length == 0) return sum;                for(int i = 0 ; i < points.length ; i ++){            HashMap
map = new HashMap<>(); for(int j = 0; j < points.length; j++){ int dis = getDis(points[i], points[j]); map.put(dis, map.getOrDefault(dis,0)+1); } for(int value : map.values()){ sum += value * (value - 1); //2 combination; } map.clear(); } return sum; } public int getDis(int[] a, int[] b){ return (a[0] - b[0]) * (a[0] - b[0]) + (a[1] - b[1]) * (a[1] - b[1]); } }

 

 

 

转载于:https://www.cnblogs.com/joannacode/p/6101145.html

你可能感兴趣的文章
CCActionPageTurn3D
查看>>
python random
查看>>
esp32-智能语音-cli(调试交互命令)
查看>>
netty与MQ使用心得
查看>>
关于dl dt dd 文字过长换行在移动端显示对齐的探讨总结
查看>>
swoolefy PHP的异步、并行、高性能网络通信引擎内置了Http/WebSocket服务器端/客户端...
查看>>
Python学习笔记
查看>>
unshift()与shift()
查看>>
使用 NPOI 、aspose实现execl模板公式计算
查看>>
行为型模式:中介者模式
查看>>
How to Notify Command to evaluate in mvvmlight
查看>>
33. Search in Rotated Sorted Array
查看>>
461. Hamming Distance
查看>>
Python垃圾回收机制详解
查看>>
{面试题1: 赋值运算符函数}
查看>>
Node中没搞明白require和import,你会被坑的很惨
查看>>
Python 标识符
查看>>
Python mysql 创建连接
查看>>
企业化的性能测试简述---如何设计性能测试方案
查看>>
centos7 安装中文编码
查看>>