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 ...
随机推荐
- python 的时间复杂度
Python内置方法的时间复杂度 本文翻译自Python Wiki 本文基于GPL v2协议,转载请保留此协议. 本页面涵盖了Python中若干方法的时间复杂度(或者叫“大欧”,“Big O”).该时 ...
- bunoj 13124(数位dp)
数位dp每次都给我一种繁琐的感觉.. A - Palindromic Numbers Time Limit:2000MS Memory Limit:32768KB 64bit IO F ...
- 拨打电话<a href="tel:">跳转到邮件<a href="mailto:">
拨打电话 <a href="tel:0571866000">0571-866000</a> 跳转到邮件 <a href="mailto:jo ...
- mvn命令上传jar
开发过程中涉及到下载第三SDK包,而本身项目是基于gradle的,所以为了项目中使用sdk包,需要将包加入到自己的仓库 1.利用nexus创建自己的第三方库thirdparty 类型hosted 2. ...
- 下载tree命令的源代码 - The Tree Command for Linux Homepage
The Tree Command for Linux Homepage http://mama.indstate.edu/users/ice/tree/ [root@test ~]# ll -as m ...
- docker,构建nginx反向代理tomcat
Nginx实现负载均衡是通过配置nginx.conf来实现的,nginx.conf的全部内容如下: user nginx; worker_processes 1; error_log /var/log ...
- sql 锁类型与锁机制
SQL Server锁类型(SQL)收藏1. HOLDLOCK: 在该表上保持共享锁,直到整个事务结束,而不是在语句执行完立即释放所添加的锁. 2. NOLOCK:不添加共享锁和排它锁,当这个选项 ...
- 我的Android进阶之旅------>Android APP终极瘦身指南
首先声明,下面文字转载于: APK瘦身实践 http://www.jayfeng.com/2015/12/29/APK%E7%98%A6%E8%BA%AB%E5%AE%9E%E8%B7%B5/ APP ...
- rails timeout 异常
发现经常有”超时“的错误信息,如/usr/lib/ruby/1.8/timeout.rb:54:in `rbuf_fill': execution expired (Timeout::Error),恩 ...
- redis 笔记04 服务器、复制
服务器 1. 一个命令请求从发送到完成主要包括以下步骤: 1). 客户端将命令请求发送给服务器 2). 服务器读取命令请求,并分析出命令参数 3). 命令执行器根据参数查找命令的实现函数,然后执行实现 ...