本文主要是介绍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.
本题有点戏剧性,在本地测试代码的时候,答案并不正确,经过很长时间反复排查逻辑后最后,硬着头皮提交竟然AC了,本来还以为是我GCC-C++版本浮点型计算的精度问题,最后才发现是自己写的Point构造函数错了。
class Solution {
public:int maxPoints(vector<Point>& points) {if(points.size() < 3) return points.size();int result = 0;for(int i = 0; i < points.size(); i++) {int repeat_points = 1, line_maxpoint = 0;map<float, int> m;for (int j = i + 1; j < points.size(); j++) {if(points[j].x == points[i].x && points[j].y == points[i].y) repeat_points++;else if(points[j].x == points[i].x) m[INFINITY]++;else m[(float)(points[j].y - points[i].y) / (points[j].x - points[i].x)]++;}for(auto line : m)line_maxpoint = max(line_maxpoint, line.second);result = max(result, line_maxpoint + repeat_points);}return result;}
};
这篇关于LeetCode # 149 Max Points on a Line的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!