447. Number of Boomerangs
题目:
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]]
链接:https://leetcode.com/problems/number-of-boomerangs/#/description
3/25/2017
自己的算法超时了,看别人答案
然而,基本上除了计算距离之外,不需要记录很多,因为本身答案需要的也就是数量而不是index
有时候需要想一想有没有简单的方法,想不到再硬来。
https://discuss.leetcode.com/topic/66587/clean-java-solution-o-n-2-166ms/2
回忆着这个答案,还是错了,真正理解了一下:
注意加count和map.clear()是在每个i里都进行的。为什么?当有从一个顶点开始相同的距离时,这个顶点就是i为index的点。而另外一个点就是相同距离里的其他点,用排列数计算。
1 public int numberOfBoomerangs(int[][] points) { 2 int res = 0; 3 4 Map<Integer, Integer> map = new HashMap<>(); 5 for(int i=0; i<points.length; i++) { 6 for(int j=0; j<points.length; j++) { 7 if(i == j) 8 continue; 9 10 int d = getDistance(points[i], points[j]); 11 map.put(d, map.getOrDefault(d, 0) + 1); 12 } 13 14 for(int val : map.values()) { 15 res += val * (val-1); 16 } 17 map.clear(); 18 } 19 20 return res; 21 } 22 23 private int getDistance(int[] a, int[] b) { 24 int dx = a[0] - b[0]; 25 int dy = a[1] - b[1]; 26 27 return dx*dx + dy*dy; 28 }
更多讨论:
https://discuss.leetcode.com/category/574/number-of-boomerangs
转载于:https://www.cnblogs.com/panini/p/6621957.html
447. Number of Boomerangs相关推荐
- C#LeetCode刷题之#447-回旋镖的数量(Number of Boomerangs)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3792 访问. 给定平面上 n 对不同的点,"回旋镖&q ...
- LeetCode447. Number of Boomerangs
Description Given n points in the plane that are all pairwise distinct, a "boomerang" is a ...
- LeetCode Number of Boomerangs
题意:给出平面上的n个点,求出元组(i,j,k)的个数,其中元组满足i与j的距离与i与k距离相等,与(i,j,k)顺序有关 思路:以一个点为起点,看其它点到该点的距离及具有一样距离的个数.在统计元组个 ...
- [LeetCode] 447.回旋镖的数量(Easy)C语言题解
题目 给定平面上 n 对不同的点,"回旋镖" 是由点表示的元组 (i, j, k) ,其中 i 和 j 之间的距离和 i 和 k 之间的距离相等(需要考虑元组的顺序). 找到所有回 ...
- LeetCode 简单算法题
使用Nodejs 抓取的LeetCode 简单算法题 一步一步来,先攻破所有简单的题目,有些题目不适合使用JS解决,请自行斟酌 Letcode 简单题汇总 104. Maximum Depth of ...
- LeetCode 分类练习(四):查找2
LeetCode 分类练习(四):查找2 目录 LeetCode 分类练习(四):查找2 一.双指针(快慢指针.对撞指针) 1.快慢指针 2.对撞指针 3.滑动窗口法 二.滑动数组 三.实战1(对撞指 ...
- LeetCode github集合,附CMU大神整理笔记
Github LeetCode集合 本人所有做过的题目都写在一个java项目中,同步到github中了,算是见证自己的进步.github目前同步的题目是2020-09-17日之后写的题.之前写过的题会 ...
- 玩转算法面试LeetCode题目目录
文章目录 一.数组中的问题其实最常见 1. 如何写出正确的程序 2. 基础算法思路的应用 3. 对撞指针 4. 滑动窗口 二.查找表相关问题 三.在链表中穿针引线 四.栈.队列.优先队列 1. 栈的基 ...
- Leetcode算法题-解法转载
版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/fuxuemingzhu/article/details/85112591 作者: 负雪明烛 i ...
最新文章
- php 验证码 扩展,使用 Captcha 扩展包 为 Laravel 5 应用生成验证码
- python 全栈开发,Day116(可迭代对象,type创建动态类,偏函数,面向对象的封装,获取外键数据,组合搜索,领域驱动设计(DDD))...
- Java重载遇到泛型
- python字典和集合对象可以进行索引操作_Python中的字典跟集合整理笔记
- C语言实践作业报告,c语言社会实践报告
- 兼容最新PS2021 Raya Pro 5 for Mac——值得推荐的PS终极数字混合扩展面板
- spark eventLoop模型
- LBS由ip查经纬度
- 【转载】关于二叉树、四叉树和八叉树 - Chinamming的专栏 - 博客频道 - CSDN.NET
- 网规:第1章计算机网络原理-1.4网络设备与网络软件
- Java面试题超详细讲解系列之八【SQL优化篇】
- 全球前十大证券交易所在区块链领域的探索和布局
- 玩玩直播,搭建一个流媒体服务器
- 偏微分方程的数值解(二): 一维状态空间的偏微分方程的 MATLAB 解法
- android开发,动态图标,Android动态更新APP图标
- 小学生遭校长拳击内脏出血 求医救命钱遭抢(图)
- 计算机组成原理课程设计:在复杂模型机上编写机器指令与微程序计算海伦公式
- MNE官网解读《Annotating continuous data》
- java浮点数计算程序_计算器程序.可以准确进行浮点数运算
- 【完全指南】如何在Github上使用Hexo搭建自己的Blog
热门文章
- java access jdbc_Java连接Access数据库
- The size of tensor a (4) must match the size of tensor b (3) at non-singletonThe size of
- LeetCode 6060. 找到最接近 0 的数字
- LeetCode 2140. 解决智力问题(动态规划)
- python 上下文管理器、 else 块、@contextmanager
- 程序员面试金典 - 面试题 16.17. 连续数列(DP/分治)
- LeetCode 1008. 先序遍历构造二叉树(已知先序,求二叉搜索树)
- datagridview 动态插入图片_挑战一张照片制作动态PPT背景
- hive建表语句_Hive数据如何同步到MaxCompute之实践讲解
- ajax 请求svg,jQuery append 到AJAX加载的SVG问题