[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 };

 

目录
相关文章
|
3月前
leetcode-849:到最近的人的最大距离
leetcode-849:到最近的人的最大距离
16 0
【LeetCode】149. 直线上最多的点数
【LeetCode】149. 直线上最多的点数
【LeetCode】149. 直线上最多的点数
|
存储 Python
LeetCode 120. 三角形最小路径和
给定一个三角形 triangle ,找出自顶向下的最小路径和。
90 0
20天刷题计划-120. 三角形最小路径和
给定一个三角形 triangle ,找出自顶向下的最小路径和。 每一步只能移动到下一行中相邻的结点上。相邻的结点 在这里指的是 下标 与 上一层结点下标 相同或者等于 上一层结点下标 + 1 的两个结点。也就是说,如果正位于当前行的下标 i ,那么下一步可以移动到下一行的下标 i 或 i + 1 。
Leetcode120三角形最小路径和
Leetcode120三角形最小路径和
67 0
|
Java
HDU 2080 夹角有多大II
夹角有多大II Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 10901    Accepted Submission(s): 5642 Problem Description 这次xhd面临的问题是这样的:在一个平面内有两个点,求两个点分别和原点的连线的夹角的大小。
930 0

热门文章

最新文章