当前位置:网站首页>LeetCode149-直线上最多的点数-数学-哈希表
LeetCode149-直线上最多的点数-数学-哈希表
2022-04-23 14:48:00 【李烦烦搞快点】
Note:
枚举所有的点,让他作为原点
选定原点之后,再去找一遍所有的点,这时候分为三种情况
1、与当前原点重合的点(可以看做任意斜率直线上面的点)
2、垂直直线上的点(这时候斜率是算不了的,除数不能为0)
3、正常的点,构成一条斜率存在的直线的点
然后每个点按照斜率来分类,最后首先检查一下斜率正常的点和垂直直线上的点谁多
然后在看看 答案 和 这次遍历后比较多的点 + 重合的点 谁多
第二次遍历的时候,肯定会有个重合的点啊,就是当前的点,这不是重复了?
是在第二次遍历的时候才计数的,不会算多的
代码如下:
class Solution {
public:
int maxPoints(vector<vector<int>>& points) {
int ans = 0;
for(auto& p: points){
int s = 0, v = 0;
unordered_map<long double, int> cnt;
for(auto& q : points){
if(p == q) s ++;
else if(p[0] == q[0]) v ++;
else{
long double k = (long double) (q[1] - p[1]) / (q[0] - p[0]);
cnt[k] ++;
}
}
int c = v;
for(auto [k, v] : cnt) c = max(c, v);
ans = max(ans, c + s);
}
return ans;
}
};
版权声明
本文为[李烦烦搞快点]所创,转载请带上原文链接,感谢
https://blog.csdn.net/Mr_Ghost812/article/details/124348976
边栏推荐
- ASEMI超快恢复二极管与肖特基二极管可以互换吗
- QT interface optimization: double click effect
- L'externalisation a duré quatre ans.
- 利用 MATLAB 编程实现最速下降法求解无约束最优化问题
- 抑郁症治疗的进展
- Achievements in science and Technology (21)
- Alexnet model
- OC to swift conditional compilation, marking, macro, log, version detection, expiration prompt
- [jz46 translate numbers into strings]
- Role of asemi rectifier module mdq100-16 in intelligent switching power supply
猜你喜欢

你還不知道責任鏈模式的使用場景嗎?

外包干了四年,废了...
![[stc8g2k64s4] introduction of comparator and sample program of comparator power down detection](/img/8c/e72e628a44a36bfc7406a002d00215.png)
[stc8g2k64s4] introduction of comparator and sample program of comparator power down detection

Explain TCP's three handshakes in detail

Set up an AI team in the game world and start the super parametric multi-agent "chaos fight"

Mq-2 and DS18B20 fire temperature smoke alarm system design, 51 single chip microcomputer, with simulation, C code, schematic diagram, PCB, etc

Find daffodils - for loop practice

Thread synchronization, life cycle

QT actual combat: Yunxi calendar

Multisim Simulation Design of DC adjustable regulated power supply of LM317 (with simulation + paper + reference)
随机推荐
压缩映射定理
Contraction mapping theorem
你还不知道责任链模式的使用场景吗?
Master in minutes --- ternary operator (ternary operator)
1-初识Go语言
SHT11传感器的温度湿度监控报警系统单片机Proteus设计(附仿真+论文+程序等)
在游戏世界组建一支AI团队,超参数的多智能体「大乱斗」开赛
Detailed comparison between asemi three-phase rectifier bridge and single-phase rectifier bridge
select 同时接收普通数据 和 带外数据
vscode中文插件不生效问题解决
Advanced application of I / O multiplexing: Processing TCP and UDP services at the same time
基于单片机的DS18B20的数字温度监控报警系统设计【LCD1602显示+Proteus仿真+C程序+论文+按键设置等】
How do I open the win10 startup folder?
QT actual combat: Yunxi calendar
SVN详细使用教程
Electronic scale weighing system design, hx711 pressure sensor, 51 single chip microcomputer (proteus simulation, C program, schematic diagram, thesis and other complete data)
Swift - Literal,字面量协议,基本数据类型、dictionary/array之间的转换
1n5408-asemi rectifier diode
Brute force of DVWA low -- > High
拼接hql时,新增字段没有出现在构造方法中
