当前位置:网站首页>LeetCode_哈希表_困难_149. 直线上最多的点数
LeetCode_哈希表_困难_149. 直线上最多的点数
2022-07-05 20:57:00 【小城老街】
1.题目
给你一个数组 points ,其中 points[i] = [xi, yi] 表示 X-Y 平面上的一个点。求最多有多少个点在同一条直线上。
示例 1:
输入:points = [[1,1],[2,2],[3,3]]
输出:3
示例 2:
输入:points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
输出:4
提示:
1 <= points.length <= 300
points[i].length == 2
-104 <= xi, yi <= 104
points 中的所有点互不相同
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/max-points-on-a-line
2.思路
(1)暴力穷举法
暴力穷举法比较容易想到,由于在平面中两个不同的点可以确定一条直线,所以可以先穷举两个点的组合(即确定一条直线),然后再检查其余的点是否在该直线上。
(2)暴力穷举法改进_哈希表
思路参考该用户题解。
由于两点确定一条直线,所以可以先穷举所有可能出现的直线斜率(即枚举所有的点对),然后使用哈希表统计所有斜率对应的直线上点的数量(即键/值 = 斜率/点的数量),那么所有值中的最大值即为答案。
3.代码实现(Java)
//思路1————暴力穷举法
class Solution {
public int maxPoints(int[][] points) {
//最多有 cnt 个点在同一条直线上,由于 1 <= points.length <= 300,所以 cnt 的初始值为 1
int cnt = 1;
int n = points.length;
for (int i = 0; i < n; i++) {
int[] p1 = points[i];
for (int j = i + 1; j < n; j++) {
int[] p2 = points[j];
//p1 与 p2 一定在一条直线上,故至少有 2 个点在同一条直线上
int tmpCnt = 2;
for (int k = j + 1; k < n; k++) {
int[] p3 = points[k];
/* (1) 如果 (p1[1] - p2[1])/(p1[0] - p2[0]) == (p2[1] - p3[1])/(p2[0] - p3[0]) 那么 p1、p2、p3 这 3 个点在同一条直线上 (2) 为了防止整数相除时有精度损失,所以将上面的等式改为: (p1[0] - p2[0]) * (p2[1] - p3[1]) = (p1[1] - p2[1]) * (p2[0] - p3[0]) */
if ((p1[0] - p2[0]) * (p2[1] - p3[1]) == (p1[1] - p2[1]) * (p2[0] - p3[0])) {
tmpCnt++;
}
}
cnt = Math.max(tmpCnt, cnt);
}
}
return cnt;
}
}
//思路2————暴力穷举法改进_哈希表
class Solution {
public int maxPoints(int[][] ps) {
int n = ps.length;
int cnt = 1;
for (int i = 0; i < n; i++) {
Map<String, Integer> hashMap = new HashMap<>();
// 由当前点 i 发出的直线所经过的最多点数量
int max = 0;
for (int j = i + 1; j < n; j++) {
int x1 = ps[i][0], y1 = ps[i][1], x2 = ps[j][0], y2 = ps[j][1];
int a = x1 - x2, b = y1 - y2;
int k = gcd(a, b);
String key = (a / k) + "_" + (b / k);
hashMap.put(key, hashMap.getOrDefault(key, 0) + 1);
max = Math.max(max, hashMap.get(key));
}
cnt = Math.max(cnt, max + 1);
}
return cnt;
}
//求 a 和 b 的最大公约数
public int gcd(int a, int b) {
return b == 0 ? a : gcd(b, a % b);
}
}
边栏推荐
- Duchefa丨MS培养基含维生素说明书
- 概率论机器学习的先验知识(上)
- Simple getting started example of Web Service
- Interpreting the daily application functions of cooperative robots
- Binary search
- Abnova CRISPR spcas9 polyclonal antibody protocol
- Cutting edge technology for cultivating robot education creativity
- CLion配置visual studio(msvc)和JOM多核编译
- How to renew NPDP? Here comes the operation guide!
- Is Kai Niu 2980 useful? Is it safe to open an account
猜你喜欢

Abnova maxpab mouse derived polyclonal antibody solution

基於flask寫一個接口

PVC 塑料片BS 476-6 火焰传播性能测定

How to make ERP inventory accounts of chemical enterprises more accurate

教你自己训练的pytorch模型转caffe(三)

Abnova丨 MaxPab 小鼠源多克隆抗体解决方案

PHP反序列化+MD5碰撞

10000+ 代码库、3000+ 研发人员大型保险集团的研发效能提升实践

研学旅游实践教育的开展助力文旅产业发展

CADD course learning (7) -- Simulation of target and small molecule interaction (semi flexible docking autodock)
随机推荐
Duchefa low melting point agarose PPC Chinese and English instructions
Abnova丨E (DIII) (WNV) 重组蛋白 中英文说明书
2.<tag-哈希表, 字符串>补充: 剑指 Offer 50. 第一个只出现一次的字符 dbc
当用户登录,经常会有实时的下拉框,例如,输入邮箱,将会@qq.com,@163.com,@sohu.com
Hdu2377bus pass (build more complex diagram +spfa)
Abnova丨DNA 标记高质量控制测试方案
Norgen AAV extractant box instructions (including features)
教你自己训练的pytorch模型转caffe(一)
poj 3414 Pots (bfs+线索)
Specification of protein quantitative kit for abbkine BCA method
显示屏DIN 4102-1 Class B1防火测试要求
请查收.NET MAUI 的最新学习资源
获取前一天的js(时间戳转换)
树莓派4B上ncnn转换出来的模型调用时总是崩溃(Segment Fault)的原因
示波器探头对信号源阻抗的影响
Monorepo management methodology and dependency security
国外LEAD美国简称对照表
wpf 获取datagrid 中指定行列的DataGridTemplateColumn中的控件
AITM2-0002 12s或60s垂直燃烧试验
Wanglaoji pharmaceutical's public welfare activity of "caring for the most lovely people under the scorching sun" was launched in Nanjing