leetcode447
public class Solution
{
/// <summary>
/// 计算两个点的距离
/// </summary>
/// <param name="x1"></param>
/// <param name="y1"></param>
/// <param name="x2"></param>
/// <param name="y2"></param>
/// <returns></returns>
private double getDistance(int x1, int y1, int x2, int y2)
{
var dis = (y2 - y1) * (y2 - y1) + (x2 - x1) * (x2 - x1);
return dis;
} /// <summary>
/// 计算n的阶乘
/// </summary>
/// <param name="n"></param>
/// <returns></returns>
private int SetpNum(int n)
{
int sum = ;
while (n != )
{
sum *= n;
n = n - ;
}
return sum;
} public int NumberOfBoomerangs(int[,] points)
{
var pointcount = points.GetLength();//点个数
var dim = points.GetLength();//每组个点的坐标维数
List<KeyValuePair<int, int>> list = new List<KeyValuePair<int, int>>(); for (int i = ; i < pointcount; i++)
{
var point = new KeyValuePair<int, int>(points[i, ], points[i, ]);
list.Add(point);
} //var dic = new Dictionary<KeyValuePair<int, int>, Dictionary<double, List<KeyValuePair<int, int>>>>();
var dic = new Dictionary<KeyValuePair<int, int>, Dictionary<double, int>>(); for (int i = ; i < list.Count; i++)
{
for (int j = i; j < list.Count; j++)
{
if (i != j)
{
var curentPoint = list[i];//主点
var otherPoint = list[j];//从点 var dis = getDistance(list[i].Key, list[i].Value, list[j].Key, list[j].Value);//dis值是同一个 #region 主点处理
if (!dic.ContainsKey(curentPoint))
{
//dic.Add(curentPoint, new Dictionary<double, List<KeyValuePair<int, int>>>());
//dic[curentPoint].Add(dis, new List<KeyValuePair<int, int>>());
//dic[curentPoint][dis].Add(list[j]); dic.Add(curentPoint, new Dictionary<double, int>());
dic[curentPoint].Add(dis, );
}
else
{
if (!dic[curentPoint].ContainsKey(dis))
{
//dic[curentPoint].Add(dis, new List<KeyValuePair<int, int>>());
//dic[curentPoint][dis].Add(list[j]); dic[curentPoint].Add(dis, );
}
else
{
//dic[curentPoint][dis].Add(list[j]); dic[curentPoint][dis]++;
}
}
#endregion 主点处理 #region 从点处理
if (!dic.ContainsKey(otherPoint))
{
//dic.Add(otherPoint, new Dictionary<double, List<KeyValuePair<int, int>>>());
//dic[otherPoint].Add(dis, new List<KeyValuePair<int, int>>());
//dic[otherPoint][dis].Add(list[i]); dic.Add(otherPoint, new Dictionary<double, int>());
dic[otherPoint].Add(dis, );
}
else
{
if (!dic[otherPoint].ContainsKey(dis))
{
//dic[otherPoint].Add(dis, new List<KeyValuePair<int, int>>());
//dic[otherPoint][dis].Add(list[i]); dic[otherPoint].Add(dis, );
}
else
{
//dic[otherPoint][dis].Add(list[i]); dic[otherPoint][dis]++;
}
} #endregion 从点处理
}
}
} var sum = ; foreach (var d in dic)
{
foreach (var d2 in d.Value)
{
if (d2.Value > )
{
var ct = d2.Value;
//计算从Count中任取2个的排列数即A(count)(2)
sum += ct * (ct - );
}
}
} return sum;
}
}
https://leetcode.com/problems/number-of-boomerangs/#/description
leetcode447的更多相关文章
- [Swift]LeetCode447. 回旋镖的数量 | Number of Boomerangs
Given n points in the plane that are all pairwise distinct, a "boomerang" is a tuple of po ...
- LeetCode447. Number of Boomerangs
Description Given n points in the plane that are all pairwise distinct, a "boomerang" is a ...
- Leetcode447.Number of Boomerangs回旋镖的数量
给定平面上 n 对不同的点,"回旋镖" 是由点表示的元组 (i, j, k) ,其中 i 和 j 之间的距离和 i 和 k 之间的距离相等(需要考虑元组的顺序). 找到所有回旋镖的 ...
随机推荐
- hdu1224 dp(dp + 栈/父亲数组记录路径)
题意:给定 n 个城市的有趣度,并给出可以从那些城市飞到那些城市.其中第一个城市即起始城市同样也作为终点城市,有趣度为 0,旅行途中只允许按有趣度从低到高旅行,问旅行的有趣度最大是多少,并输出旅行路径 ...
- Mybatis的mapper文件中$和#的用法及区别详解
https://www.2cto.com/database/201806/752139.html用了一段时间的Mybatis了,对于$和#的用法老是很迷糊,特此记下加深记忆. 简单来说 #{} 会在将 ...
- 【HAOI2013】花卉节
HA果然是弱省中的弱省…… 原题: ZZ市准备在绿博园举办一次花卉节.Dr.Kong接受到一个任务,要买一批花卉进行布置园林.能投入买花卉的资金只有B元 (1 <= B <= 10^18) ...
- Memcached 真的过时了吗?
这两年Redis火得可以,Redis也常常被当作Memcached的挑战者被提到桌面上来.关于Redis与Memcached的比较更是比比皆是.然而,Redis真的在功能.性能以及内存使用效率上都超越 ...
- 使用lua graphql 模块让openresty 支持graphql api
graphql 是一个很不错的api 查询标准语言,已经有一个lua 的版本支持graphql 项目使用docker&&docker-compose 运行 环境准备 模块安装 lu ...
- css中display为none 和visibility为hidden的区别
区别一: display:none Turns off the display of an element (it has no effect on layout); all child eleme ...
- GridColumn (Column Layout and Auto Width)
Namespace:DevExpress.XtraGrid.Columns Assembly:DevExpress.XtraGrid.v16.2.dll https://documentation.d ...
- Codelf 变量取名
Codelf 变量取名 可以看到别的变量是怎么命名的,站在巨人的肩膀上.
- k最邻近算法——加权kNN
加权kNN 上篇文章中提到为每个点的距离增加一个权重,使得距离近的点可以得到更大的权重,在此描述如何加权. 反函数 该方法最简单的形式是返回距离的倒数,比如距离d,权重1/d.有时候,完全一样或非常接 ...
- 百度,谷歌,360,搜狗,神马等蜘蛛IP段
https://www.imydl.com/wzjs/5971.html 记得3月份的时候明月分享过一篇[站长必备:百度.谷歌.搜狗.360等蜘蛛常见IP地址]的文章,好像一直都受到了众多站长们的关注 ...