当前位置:网站首页>LeetCode 447. Number of boomerangs (permutation and combination problem)
LeetCode 447. Number of boomerangs (permutation and combination problem)
2022-04-23 02:01:00 【Currybeefer】
In general, we need to find the combination of three points ,i,j,k, For i Come on ,j and k On i The distances must be equal .
Violent enumeration requires a triple cycle , So the time complexity is O(n^3) It's not worth it .
So we can think of the permutation and combination problem , The idea is to enumerate if i Fixed point , Filter out the distance and... From the remaining points i A collection of equal points .
Suppose you're away from i A distance of a A little n individual , So it's from n Choose two points in the order and i Combine , The permutation and combination problem ,An2.
Store a hash table with a distance of a Number of combinations , Add it up .
int numberOfBoomerangs(vector<vector<int>>& points)
{
int res=0;
for(int i=0;i<points.size();i++)
{
unordered_map<int,int> map;
for(int j=0;j<points.size();j++)
{
int dis=Distance(points[i],points[j]);
map[dis]++;
}
for(auto p=map.begin();p!=map.end();p++)
{
res+=p->second*(p->second-1);
}
}
return res;
}
int Distance(vector<int> a, vector<int> b)
{
int x=a[0]-b[0];
int y=a[1]-b[1];
return x*x+y*y;
}
版权声明
本文为[Currybeefer]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220846113211.html
边栏推荐
- How to choose a good dial-up server?
- 如何选择一台好的拨号服务器?
- 一加一为什么等于二
- Implementation of Base64 encoding / decoding in C language
- What is a makefile file?
- mb_ substr()、mb_ Strpos() function (story)
- 2022第六季完美童模 IPA國民賽領跑元宇宙賽道
- Unity editor hierarchy drop-down menu extension
- K zeros after leetcode factorial function
- Challenges often faced by client project management
猜你喜欢
【dpdk】10. Dpdk DNS learning notes
单片机和4G模块通信总结(EC20)
What should I pay attention to when using proxy IP?
W801 / w800 WiFi socket development (II) - UDP Bluetooth control WiFi connection
Challenges often faced by client project management
The sixth season of 2022, the perfect children's model IPA national race leads the yuanuniverse track
Use Xdebug breakpoint debugging in postman
What problems will you encounter when dialing VPS?
Introduction to esp32 Bluetooth controller API
【动手学深度学习V2】循环神经网络-1.序列模型
随机推荐
如何选择一台好的拨号服务器?
代理IP可用率是不是等同于代理IP的效率?
使用代理IP是需要注意什么?
How to write the resume of Software Test Engineer so that HR can see it?
PHP & laravel & master several ways of generating token by API and some precautions (PIT)
Today will finally write system out. Println()
Micro build low code zero foundation introductory course
2022.4.22-----leetcode.396
什么是api接口?
English abbreviation of role personal attribute
Cc2541 emulator CC debugger tutorial
Thinkphp内核开发盲盒商城源码v2.0 对接易支付/阿里云短信/七牛云存储
FL studio20. 8 the latest Chinese version installation and download graphic tutorial
J-Link RTT使用
What problems will you encounter when dialing VPS?
What categories do you need to know before using proxy IP?
Virtual serial port function of j-link V9 using skills
[tutorial] how to use GCC "zero assembly" for white whoring MDK
电子采购如何成为供应链中的增值功能?
Makefile文件是什麼?