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的更多相关文章

  1. [Swift]LeetCode447. 回旋镖的数量 | Number of Boomerangs

    Given n points in the plane that are all pairwise distinct, a "boomerang" is a tuple of po ...

  2. LeetCode447. Number of Boomerangs

    Description Given n points in the plane that are all pairwise distinct, a "boomerang" is a ...

  3. Leetcode447.Number of Boomerangs回旋镖的数量

    给定平面上 n 对不同的点,"回旋镖" 是由点表示的元组 (i, j, k) ,其中 i 和 j 之间的距离和 i 和 k 之间的距离相等(需要考虑元组的顺序). 找到所有回旋镖的 ...

随机推荐

  1. mysql query 条件中为空时忽略

    ☆. q.ques_group传入为null或''的时候不查询此条件: value AND (q.ques_group = :quesGroup or :quesGroup is null or :q ...

  2. Python & 机器学习入门指导

    Getting started with Python & Machine Learning(阅者注:这是一篇关于机器学习的指导入门,作者大致描述了用Python来开始机器学习的优劣,以及如果 ...

  3. 表单添加缩略图及截图js代码

    此为表单提交是上传截图的代码,待优化: // 添加小程序图片 function addAvatar(obj){ var file = obj.files[0]; limit($('.avatar_bo ...

  4. SQL Server获取指定行的数据

    SQL Server获取指定行(如第二行)的数据   --SQL Server获取指定行(如第二行)的数据-- --法一(对象法)-- select * from ( select * , numbe ...

  5. NoSQLUnit

    NoSQLUnit Core Overview Unit testing is a method by which the smallest testable part of an applicati ...

  6. 前端基础之CSS快速入门

    前一篇写了我们的Html的常用组件,当然那些组件在我们不去写样式的时候都是使用的浏览器的默认样式,可以说是非常之丑到爆炸了,我们肯定是不能让用户去看这样丑到爆炸的样式,所以我们在这里需要使用css样式 ...

  7. Javascript undefined 和 null

    Javascript undefined 和 null 虽然 Javascript 一切皆对象,但是类型还是有区别的. undefined 表示 未定义的数据类型. null 表示空对象. 在判断时没 ...

  8. HBase常用指令

    disable 'smsFlow'drop 'smsFlow'create 'smsFlow','info','partition'count 'smsFlow'scan 'smsFlow' trun ...

  9. bzoj 2286(洛谷 2495) [Sdoi2011]消耗战——虚树

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2286 https://www.luogu.org/problemnew/show/P2495 ...

  10. 51nod 1934 受限制的排列——笛卡尔树

    题目:http://www.51nod.com/Challenge/Problem.html#!#problemId=1934 根据给出的信息,可以递归地把笛卡尔树建出来.一个点只应该有 0/1/2 ...