149. Max Points on a Line

Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.

要求是输入n个坐标点,输出最多有多少个点在同一条直线上。

代码提交如下:

 1 int Solution::maxPoints(vector<Point>& points)
 2 {
 3     if (points.size()<=2)
 4     {
 5         return points.size();
 6     }
 7
 8     unsigned int max_line_nums = 0;
 9
10     for (unsigned int i = 0;i < points.size(); i++)
11     {
12         slope_map same_line;
13         unsigned int same_line_nums = 0,
14                      same_x_nums = 0,
15                      same_y_nums = 0,
16                      same_points_nums = 0;
17         for (unsigned int  j = i + 1;  j < points.size(); j++)
18         {
19             if (points[i].x == points[j].x && points[i].y == points[j].y)
20             {
21                 ++same_points_nums;
22             }
23             else if (points[i].x == points[j].x)
24             {
25                 ++same_x_nums;
26             }
27             else if (points[i].y == points[j].y)
28             {
29                 ++same_y_nums;
30             }
31             else
32             {
33                 double slope = ((double)points[i].y - (double)points[j].y)/((double)points[i].x - (double)points[j].x);
34                 double b = points[i].y - slope * points[i].x;
35
36                 same_line[slope][b]++;
37                 same_line_nums = (same_line_nums < same_line[slope][b])?same_line[slope][b]:same_line_nums;
38             }
39         }
40         unsigned int tmp_max_line_nums = same_points_nums + max(same_line_nums, (same_x_nums < same_y_nums)?same_y_nums:same_x_nums);
41         max_line_nums =  tmp_max_line_nums < max_line_nums ? max_line_nums:tmp_max_line_nums;
42     }
43
44     return max_line_nums + 1;
45 }

不足之处:

利用y = kx + b 可以将k和b存在一个map中,但是对于很大的数来说,精度会引起误差,使得两个不同的点被认为是同一个点。

时间: 2024-12-19 15:02:50

149. Max Points on a Line的相关文章

149. Max Points on a Line *HARD* 求点集中在一条直线上的最多点数

Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. /** * Definition for a point. * struct Point { * int x; * int y; * Point() : x(0), y(0) {} * Point(int a, int b) : x(a), y(b) {} * }; */ class Solutio

149. Max Points on a Line (Array; Greedy)

Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. 思路:对于某一点来说,在经过该点的直线中选取节点数量最多的直线:对于全局来说,必定是某个局部点满足条件的直线之一=>局部最优解也是全局最优解=>贪心法. struct Point { int x; int y; Point() : x(0), y(0) {} Point(int a, int b)

Java for LeetCode 149 Max Points on a Line

Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. 解题思路: 本题主要需要考虑到斜线的情况,可以分别计算出过points[i]直线最多含几个点,然后算出最大即可,由于计算points[i]的时候,前面的点都计算过了,所以不需要把前面的点考虑进去,所以问题可以转化为过points[i]的直线最大点的个数,解题思路是用一个HashMap储存斜率,遍历p

【leetcode每日一题】149.Max Points on a line

题目:Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. 解析:题目要求是给定n个2维的点,求出总共有多少个点在同一条直线上.由数学知识可知,给定三个点a,b,c,如果三个点在一条直线上,则a和b的斜率与c和d的斜率是相同的.用哈希表来做,针对每个点,求出其余所有点与它的斜率,找出最大值.然后遍历所有的点,返回最大值. 特别需要主意的几点:1)可能存

leetcode[149]Max Points on a Line

Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. /** * Definition for a point. * struct Point { * int x; * int y; * Point() : x(0), y(0) {} * Point(int a, int b) : x(a), y(b) {} * }; */ class Solutio

[leedcode 149] Max Points on a Line

Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. /** * Definition for a point. * class Point { * int x; * int y; * Point() { x = 0; y = 0; } * Point(int a, int b) { x = a; y = b; } * } */ public clas

149 Max Points on a Line 直线上最多的点数

给定二维平面上有 n 个点,求最多有多少点在同一条直线上. 详见:https://leetcode.com/problems/max-points-on-a-line/description/ /** * Definition for a point. * struct Point { * int x; * int y; * Point() : x(0), y(0) {} * Point(int a, int b) : x(a), y(b) {} * }; */ class Solution {

【leetcode】149 Max Points on a Line

题目说明 https://leetcode-cn.com/problems/max-points-on-a-line/description/ 给定一个二维平面,平面上有 n 个点,求最多有多少个点在同一条直线上. 解法1 首先遍历点,在第二层循环中使用map记录倾角斜率及其匹配次数. 斜率dx/dy(dx表示两点x轴的距离,dy表示两点y轴的差值)进行计算,但是这里有一种特殊情况,就是dy=0的情况,此种情况下点也在同一直线下,所以需要单独做判断. 还有一种情况就是如果点坐标一致,则不管其他点

LeetCode: Max Points on a Line [149]

[题目] Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. [题意] 给定一堆点,要求找出一条之前上的最大点数 [思路] 没什么好的方法,从每个点P出发,遍历所有的情况 从每个点P出发,斜率相同的点即为统一之前上的点 注意两种特殊情况: 1. 两个点重合(即为同一个点) 2. 两个点的很坐标相同,即两天构成的之前垂直于X轴 [代码] /** * D