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

本文共 1338 字,大约阅读时间需要 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]]

Solution: Use HashTable, Time: O(N^2), Space: O(N)

我的:注意14行是有value个重复distance,表示有value个点,他们跟指定点距离都是distance,需要选取2个做permutation, 所以是value * (value-1)

1 public class Solution { 2     public int numberOfBoomerangs(int[][] points) { 3         int res = 0; 4         for (int i=0; i
map = new HashMap
(); 6 for (int j=0; j
1) {14 res += value * (value - 1);15 }16 }17 }18 return res;19 }20 21 public int calDistance(int[] p1, int[] p2) {22 int dx = Math.abs(p2[0] - p1[0]);23 int dy = Math.abs(p2[1] - p1[1]);24 return dx*dx + dy*dy;25 }26 }

 

别人的简洁写法

1 public int numberOfBoomerangs(int[][] points) { 2     int res = 0; 3  4     Map
map = new HashMap<>(); 5 for(int i=0; i

 

 

 

转载地址:http://eufbm.baihongyu.com/

你可能感兴趣的文章
对LigerUI控件库进行扩展,自定义extend和override,并扩展事件前与事件后
查看>>
Tengine——安装起来真费劲
查看>>
关于Oracle过程,函数的经典例子及解析
查看>>
Android-PullToRefresh(一)
查看>>
JavaScript+XML+VBA导出报表初步构想
查看>>
UVA1452|LA4727-----Jump------经典的约瑟夫公式的变形(DP)
查看>>
Android SDK安装教程
查看>>
sourceinsight 相对路径设置
查看>>
mysql describe
查看>>
程序员的自我修养 学习笔记(5)
查看>>
DNS安全浅议、域名A记录(ANAME),MX记录,CNAME记录 专题
查看>>
数据字典生成工具之旅(9):多线程使用及介绍
查看>>
Java编程思想学习笔记——注解
查看>>
使用HTML5新特性Mutation Observer实现编辑器的撤销和撤销回退操作
查看>>
Java可变参数传递中可以接收多个对象
查看>>
Python中的正则表达式(re)
查看>>
2016 新学++ , 回顾过去展望未来
查看>>
让你在DOS中任意切换目录
查看>>
较完整的轮播图特效
查看>>
微信公众开发接入服务器的接口配置信息
查看>>