447. 求回旋镖坐标的数量 NumberOfBoomerangs

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 iand 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]]

题意:定义一种类似“回形标”的三元组结构,即在三元组(i, j, k)中i和j之间的距离与i和k之间的距离相等。找到一组坐标数据中,可构成几组这样的“回形标”结构。

思路:遍历每一个点,用字典存储距离相同的点的数量

根据数量,运用排列组合公式N*(N-1) ,计算回旋镖的可组成数量

For those that don‘t understand how groupCount * (groupCount + 1) became N * (N - 1):
algorithm actually sets groupCount to zero for the first point, 1 for the second point, etc. So, if N == groupCount + 1

N * (N - 1)
== (groupCount + 1) * ((groupCount + 1) - 1)
== (groupCount + 1) * (groupCount)
== groupCount * (groupCount + 1)
  1. static public int NumberOfBoomerangs(int[,] points) {
  2. int number = 0;
  3. int disSqrt = 0;
  4. int count = points.GetLength(0);
  5. for (int i = 0; i < count; i++) {
  6. Dictionary<int, int> dic = new Dictionary<int, int>();
  7. for (int j = 0; j < count; j++) {
  8. if (i == j) {
  9. continue;
  10. }
  11. disSqrt = (points[i, 0] - points[j, 0]) * (points[i, 0] - points[j, 0]) -
  12. (points[i, 1] - points[j, 1]) * (points[i, 1] - points[j, 1]);
  13. if (dic.ContainsKey(disSqrt)) {
  14. dic[disSqrt]++;
  15. } else {
  16. dic[disSqrt] = 0;
  17. }
  18. }
  19. foreach (int value in dic.Values) {
  20. number += value * (value + 1);
  21. }
  22. }
  23. return number;
  24. }

https://discuss.leetcode.com/topic/67102/c-hashtable-solution-o-n-2-time-o-n-space-with-explaination/2

https://discuss.leetcode.com/topic/68139/simple-java-solution-using-hashmap-beats-90/3

来自为知笔记(Wiz)

时间: 2024-08-14 22:17:02

447. 求回旋镖坐标的数量 NumberOfBoomerangs的相关文章

分别利用并查集,DFS和BFS方法求联通块的数量

联通块是指给定n个点,输入a,b(1<=a,b<=n),然后将a,b连接,凡是连接在一起的所有数就是一个联通块: 题意:第一行输入n,m,分别表示有n个数,有输入m对连接点,以下将要输入m行(输入数据到文件截止): 输出:第一行要求输出联通块的个数,并在第二行分别输出每个联通块中点的数量,每个数之间以一个空格隔开. 样例 15 31 42 53 5输出:2 2 3样列2 9 81 22 33 43 74 54 67 87 9输出: 19 如果不明白的话可以画图试试,最多花半个小时,要是早这样不

c语言求平面上2个坐标点的直线距离、求俩坐标直线距离作为半径的圆的面积、递归、菲波那次数列、explode

1 #include <stdio.h> 2 #include <math.h> 3 #include <string.h> 4 5 char explode( char * str , char symbol ); 6 7 8 double distance ( int x1 , int y1 , int x2 , int y2 ); // 求平面上2个坐标点的直线距离 9 double circle_area( double radius ); // 求圆面积. r

求拓扑排序的数量,例题 topcoder srm 654 div2 500

周赛时遇到的一道比较有意思的题目: Problem Statement      There are N rooms in Maki's new house. The rooms are numbered from 0 to N-1. Some pairs of rooms are connected by bidirectional passages. The passages have the topology of a tree. That is, there are exactly N-

求图中欧拉回路数量

问题描述:输入点数n.边数m.每条边连接的两个点a,b,求此无向图中不包括一个点本身的欧拉回路数量,重复不计: 解决此问题,则需要在深搜的基础上判断新搜出的欧拉回路是否已经被走过,可以利用搜索时所打的标记来判断: 使用邻接表,需要注意在无向图中连接两点有两条边,要同时考虑: 以下为代码: #include<iostream>#include<cstdio>#include<cstring>using namespace std;int n,m,cnt[105],ct,q

Gym - 100342J:Triatrip(Bitset加速求三元环的数量)

题意:求有向图里面有多少个三元环. 思路:枚举起点A,遍历A可以到的B,然后求C的数量,C的数量位B可以到是地方X集合,和可以到A的地方Y集合的交集(X&Y). B点可以枚举,也可以遍历.(两种都试过,区别不大.) 枚举代码: #include<cstdio> #include<bitset> #include<cstdlib> #include<cstring> #include<iostream> #include<algori

算法学习——利用归并排序求逆序对的数量

首先明白逆序对的定义,逆序对就是数组中两个元素前大后小,我们就称这两个元素为一组逆序对. 接着看题目:  我们利用分治的思想,将区间一分为二,然后得到了逆序对的存在情况共三种: 1.两个元素都在左侧区间. 2.两个元素都在右侧区间. 3.两个元素一个在左,一个在右. 那么很明显我们分治的去解决这个问题,就得到解法, 1.将区间划分成左右两个区间 2.递归左右两个区间 3.统计逆序对数量(1)+(2) 4.计算逆序对数量(3) 5.返回相应的结果. 那么问题来了,逆序对(1)(2)的情况都很容易理

poj 1269 Intersecting Lines(判断两直线关系,并求交点坐标)

Intersecting Lines Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 12421   Accepted: 5548 Description We all know that a pair of distinct points on a plane defines a line and that a pair of lines on a plane will intersect in one of three

三角形已知三个点坐标,求外心坐标的公式

设三个点坐标为:(x1, y1), (x2, y2), (x3, y3).外心坐标为:(a, b). 原文地址:https://www.cnblogs.com/565261641-fzh/p/8111671.html

poj3376 KMP+字典树求回文串数量(n*n)

Finding Palindromes Time Limit: 10000MS   Memory Limit: 262144K Total Submissions: 4043   Accepted: 746 Case Time Limit: 2000MS Description A word is called a palindrome if we read from right to left is as same as we read from left to right. For exam