当前位置:网站首页>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
边栏推荐
- Chapter 7 of JVM series -- bytecode execution engine
- 8.5 循环神经网络简洁实现
- 【STC8G2K64S4】比较器介绍以及比较器掉电检测示例程序
- 3、 Gradient descent solution θ
- A good tool: aardio
- Progress in the treatment of depression
- epoll 的EPOLLONESHOT 事件———实例程序
- QT Detailed explanation of pro file
- Unity_代码方式添加绑定按钮点击事件
- Resolve the conflict between computed attribute and input blur event
猜你喜欢
GIS数据处理-cesium中模型位置设置
Sword finger offer II 019 Delete at most one character to get palindrome (simple)
利用 MATLAB 编程实现最速下降法求解无约束最优化问题
Mds55-16-asemi rectifier module mds55-16
3、 Gradient descent solution θ
机器学习之逻辑回归(Logistic Regression)原理讲解和实例应用,果断收藏
编程哲学——自动加载、依赖注入与控制反转
OC 转 Swift 条件编译、标记、宏、 Log、 版本检测、过期提示
Swift protocol Association object resource name management multithreading GCD delay once
eolink 如何助力远程办公
随机推荐
Model location setting in GIS data processing -cesium
epoll 的 ET,LT工作模式———实例程序
The initial C language framework is suitable for review and preliminary understanding
一款不错的工具:aardio
A blog allows you to learn how to write markdown on vscode
QT interface optimization: double click effect
Electronic perpetual calendar of DS1302_ 51 single chip microcomputer, month, day, week, hour, minute and second, lunar calendar and temperature, with alarm clock and complete set of data
Detailed explanation of C language P2 selection branch statement
Bingbing learning notes: take you step by step to realize the sequence table
MySQL error packet out of order
【工厂模式详解】工厂方法模式
全连接层的作用是什么?
自动化的艺术
pnpm安装使用
PWM speed regulation control system of DC motor based on 51 single chip microcomputer (with complete set of data such as Proteus simulation + C program)
一个月把字节,腾讯,阿里都面了,写点面经总结……
3、 Gradient descent solution θ
Advanced application of I / O multiplexing: Processing TCP and UDP services at the same time
解决computed属性与input的blur事件冲突问题
Matlab Simulink modeling and design of single-phase AC-AC frequency converter, with MATLAB simulation, PPT and papers