LeetCode——Number of Boomerangs
LeetCode——Number of Boomerangs
Question
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:
2
Explanation:
The two boomerangs are [[1,0],[0,0],[2,0]] and [[1,0],[2,0],[0,0]]
解题思路
注意这道题只需要求有多少对,没有具体要求是哪些,所以可以只记录点之间的距离。可以考虑用一个hash table记录每一个点到其他所有点的距离,如果相同距离数目大于等于2, 那么必有满足要求的组合。就是最后的时候注意如何求这样的组合。假如距离为6的点对是a, 那么这样的对数就是: (a * (a - 1) / 2) * 2.
具体实现
#include <iostream>
#include <vector>
#include <map>
using namespace std;
class Solution {
public:
int numberOfBoomerangs(vector<pair<int, int>>& points) {
int res = 0;
for (int i = 0; i < points.size(); i++) {
map<int, int> dis_map;
for (int j = 0; j < points.size(); j++) {
if (i != j) {
int dis = distance(points[i], points[j]);
dis_map[dis]++;
}
}
for (map<int, int>::iterator it = dis_map.begin(); it != dis_map.end(); it++) {
res += (it->second * (it->second - 1) / 2) * 2;
}
}
return res;
}
int distance(pair<int, int>& a, pair<int, int>& b) {
int x = (a.first - b.first) * (a.first - b.first);
int y = (a.second - b.second) * (a.second - b.second);
return x + y;
}
};
LeetCode——Number of Boomerangs的更多相关文章
- [LeetCode] Number of Boomerangs 回旋镖的数量
Given n points in the plane that are all pairwise distinct, a "boomerang" is a tuple of po ...
- Leetcode: Number of Boomerangs
Given n points in the plane that are all pairwise distinct, a "boomerang" is a tuple of po ...
- Python3解leetcode Number of Boomerangs
问题描述: Given n points in the plane that are all pairwise distinct, a "boomerang" is a tuple ...
- LeetCode 447. Number of Boomerangs (回力标的数量)
Given n points in the plane that are all pairwise distinct, a "boomerang" is a tuple of po ...
- 【LeetCode】447. Number of Boomerangs 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 [LeetCode] 题目地址:https:/ ...
- [LeetCode]447 Number of Boomerangs
Given n points in the plane that are all pairwise distinct, a "boomerang" is a tuple of po ...
- 34. leetcode 447. Number of Boomerangs
Given n points in the plane that are all pairwise distinct, a "boomerang" is a tuple of po ...
- [LeetCode&Python] Problem 447. Number of Boomerangs
Given n points in the plane that are all pairwise distinct, a "boomerang" is a tuple of po ...
- C#LeetCode刷题之#447-回旋镖的数量(Number of Boomerangs)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3792 访问. 给定平面上 n 对不同的点,"回旋镖&q ...
随机推荐
- JZOJ.5285【NOIP2017模拟8.16】排序
Description
- List 接口常用子类及其特点
List 常用子类: - Vector: 内部是数组数据结构,是同步的. 增删, 查询都很慢 - ArrayList: 内部是数组数据结构,是不同步的,替代了 Vector,不同步的效率较高. 特点: ...
- 解决ajax post json数据 后端无法收到的问题
如图,想把input框中的文字以json格式post出去,结果后端收不到 使用wireshark抓包,根本没有抓到发往服务器的包,说明错误在前端. 后来发现ajax post json数据的时候key ...
- 我的Android进阶之旅------>Android Studio 快捷键整理分享
正式转战Android Studio了,首先把Android Studio的快捷键摘录下来,以备后用. (官网的快捷键列表如下 https://developer.android.com/studi ...
- android应用程序优化之布局优化
在我们开发APP时不仅要在代码实现上.做到对App的优化,而在我们的界面布局也有很多要优化的地方,假设布局写的非常low的话,系统载入布局的速度会十分的慢,使得用户的体验非常的不好.这篇文章主要是从我 ...
- 目标检测之R-FCN
R-FCN:Object Detection via Region-based Fully Convolutional Networks R-FCN的网络结构 一个Base的convolutional ...
- tomcat 日志目录 介绍
[root@mysql tomcat]# ll 总用量 drwxr-x---. root root 11月 : bin -rw-r-----. root root 11月 : BUILDING.txt ...
- 如何使用django中的cookie和session?
1.Cookie 介绍 Cookie是由服务器端生成,发送给User-Agent(一般是浏览器),浏览器会将Cookie的key/value保存到某个目录下的文本文件内,下次请求同一网站时就发送该Co ...
- 转:[NHibernate]视图处理
转自:http://www.cnblogs.com/wolf-sun/p/4082899.html 目录 写在前面 文档与系列文章 视图 一个例子 总结 写在前面 前面的文章主要讲了对物理数据表的操作 ...
- 转:MVC遇上bootstrap后的ajax表单验证
使用bootstrap后他由他自带的样式has-error,想要使用它就会比较麻烦,往常使用jqueyr.validate的话只有使用他自己的样式了,而且有模型在使用模型验证更方便点.怎么解决呢? 当 ...