[LintCode] 最多有多少个点在一条直线上

简介: 1 /** 2 * Definition for a point. 3 * struct Point { 4 * int x; 5 * int y; 6 * Point() : x(0), y(0) {} 7 * Point(...
 1 /**
 2  * Definition for a point.
 3  * struct Point {
 4  *     int x;
 5  *     int y;
 6  *     Point() : x(0), y(0) {}
 7  *     Point(int a, int b) : x(a), y(b) {}
 8  * };
 9  */
10 class Solution {
11 public:
12     /**
13      * @param points an array of point
14      * @return an integer
15      */
16     int maxPoints(vector<Point>& points) {
17         // Write your code here
18         unordered_map<float, int> slopes;
19         int maxp = 0, n = points.size();
20         for (int i = 0; i < n; i++) {
21             slopes.clear();
22             int duplicate = 1;
23             for (int j = i + 1; j < n; j++) {
24                 if (points[i].x == points[j].x && points[i].y == points[j].y) {
25                     duplicate++;
26                     continue;
27                 }
28                 float slope = (points[i].x == points[j].x) ? INT_MAX:
29                               (float)(points[i].y - points[j].y) / (points[i].x - points[j].x);
30                 slopes[slope]++;
31             }
32             maxp = max(maxp, duplicate);
33             for (auto slope : slopes)
34                 if (slope.second + duplicate > maxp)
35                     maxp = slope.second + duplicate;
36         }
37         return maxp;
38     }
39 };

 

目录
相关文章
|
存储 Python
海明距离(Hamming Distance)
海明距离(Hamming Distance)是用来衡量两个二进制数之间差异程度的指标,它表示两个二进制数之间最多有多少个比特的差异。海明距离可以用于衡量数据传输或存储中的错误率,以及检测噪声干扰。 海明距离的计算方法是:对于两个 n 位二进制数,将它们进行逐位比较,如果对应位上的数字不同,则计算距离时增加 1。然后将所有位上的距离加在一起,得到海明距离。
1895 1
LeetCode 223. 矩形面积
LeetCode 223. 矩形面积
85 0
五种常用距离的代码实现:欧式距离、曼哈顿距离、闵可夫斯基距离、余弦相似度、杰卡德距离
五种常用距离的代码实现:欧式距离、曼哈顿距离、闵可夫斯基距离、余弦相似度、杰卡德距离
|
9月前
|
算法
[Halcon&拟合] 直线、矩形和圆的边缘提取
[Halcon&拟合] 直线、矩形和圆的边缘提取
523 0
|
9月前
|
Serverless C++
[C++/PTA] 计算点到直线的距离一一友元函数的应用
[C++/PTA] 计算点到直线的距离一一友元函数的应用
192 0
|
机器学习/深度学习 算法 搜索推荐
曼哈顿距离(Manhattan distance)
曼哈顿距离(Manhattan distance),也称为城市街区距离(City block distance)或L1距离(L1 distance),是两个点在标准坐标系上的绝对值距离之和。
1095 4
|
机器学习/深度学习 算法 Python
欧几里得距离(Euclidean distance)
欧几里得距离(Euclidean distance)是在数学中常用于衡量两个点之间的距离的一种方法。它在几何学和机器学习等领域都有广泛的应用。欧几里得距离基于两点之间的直线距离,可以看作是在一个多维空间中测量两个点之间的直线距离。
805 1
|
算法
巧解“求取矩形面积划分”
巧解“求取矩形面积划分”
129 0
【LeetCode】149. 直线上最多的点数
【LeetCode】149. 直线上最多的点数
【LeetCode】149. 直线上最多的点数
AcWing 604. 圆的面积
AcWing 604. 圆的面积
88 0
AcWing 604. 圆的面积