149. 直线上最多的点数
149. 直线上最多的点数
给你一个数组 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
中的所有点 互不相同
解析:
两点式(x - x1) * (y2 - y1) = (y - y1) * (x2 - x1),然后暴力就好了
class Solution { public: int maxPoints(vector<vector<int>>& points) { if(points.size() == 1) return 1; int max_cnt = 0; for(int i = 0; i < points.size(); i++) { int x1 = points[i][0], y1 = points[i][1]; for(int j = i + 1; j < points.size(); j++) { int x2 = points[j][0], y2 = points[j][1]; int cnt = 0; for(int k = j + 1; k < points.size(); k++) { int x = points[k][0], y = points[k][1]; if((x - x1) * (y2 - y1) == (y - y1) * (x2 - x1)) cnt++; } max_cnt = max(max_cnt, cnt + 2); } } return max_cnt; } };
自己选择的路,跪着也要走完。朋友们,虽然这个世界日益浮躁起来,只要能够为了当时纯粹的梦想和感动坚持努力下去,不管其它人怎么样,我们也能够保持自己的本色走下去。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 终于写完轮子一部分:tcp代理 了,记录一下
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
2021-09-12 python sort
2021-09-12 直方图 与 均衡化
2018-09-12 Berland and the Shortest Paths CodeForces - 1005F(最短路树)
2018-09-12 Allowed Letters CodeForces - 1009G(状压思维)
2018-09-12 Military Problem CodeForces - 1006E(dfs搜一下 标记一下)