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 之间的距离相等(需要考虑元组的顺序). 找到所有回旋镖的 ...
随机推荐
- linux和mac使用virtualenv使用和安装
virtualenv是python的三大神器之一,用于创建独立的python虚拟环境,多个python版本相互独立,互不影响,可以在一台电 脑上同时安装多个版本的python,而且不影响本机pytho ...
- ActiveMQ默认协议和IO模型优化
在ActiveMQ的官方网站上,列出了目前ActiveMQ中支持的所有消息协议,它们是:AMQP.MQTT.OpenWire.REST.Stomp.XMPP: 不同的协议需要设置不同的网络监听端口,这 ...
- 【转】解决Win7字体模糊不清晰的最佳办法
原文网址:http://blog.sina.com.cn/s/blog_3d5f68cd0100ldtp.html 相信初次用win7的朋友,都会遇到字体不清晰的问题,有很多人因为这个问题而放弃使用w ...
- 前端可视化建模技术概览,包括:GoJS
我推荐使用的: 库 网址 备注 GoJS https://gojs.net/latest/samples/flowchart.html 推荐使用 相关文章: 前端可视化建模技术概览:http://le ...
- 一个经典的PHP加密解密算法
项目中有时我们需要使用PHP将特定的信息进行加密,也就是通过加密算法生成一个加密字符串,这个加密后的字符串可以通过解密算法进行解密,便于程序对解密后的信息进行处理.最常见的应用在用户登录以及一些API ...
- Microsoft Dynamics CRM 2011 面向Internet部署 (IFD) CRM 登录出现会话超时的解决办法
一.IFD 登录的时候,过了一段时间,会马上出现“您的会话已过期”,怎么解决这个问题呢,可以通过改变这个时间.具体图如二 Link to Dynamics CRM Wiki Home Page 二.S ...
- oracle schema 白话文详解
概述: (一)什么Oracle叫用户(user): A user is a name defined in the database that can connect to and access ob ...
- 关闭IE 对剪切板访问的提示
在internet 选项-“安全”选项卡-自定义级别. 在“脚本”下面找到“允许对剪切板进行编程访问”,选择“启用”即可. -END
- file_get_contents failed to open stream: HTTP request failed(一个字符决定成败)
file_get_contents 开始成功了.后来不知怎么的就报错,调试半天没发现什么问题. 结果.里面多了一个"空格"就报错.去掉空格就好了,你们仔细找找吧.
- git: fatal: Could not read from remote repository
This is probably an Intellij problem. Your key are managed natively by ssh, and Intellij has it's ow ...