LeetCode | Max Points on a Line
来源:互联网 发布:淘宝好吃的零食推荐 编辑:程序博客网 时间:2024/06/11 18:29
题目:
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 Solution {public: int maxPoints(vector<Point> &points) { if(points.size() <= 2){ return points.size(); } int max = 2; int cur = 1; for(int i = 0; i < points.size(); i++){ int duplicate = 0; for(int j = i + 1; j < points.size(); j++){ if(points[i].x == points[j].x && points[i].y == points[j].y){ duplicate++; continue; } cur++; for(int k = j + 1; k < points.size(); k++){ if(sameLine(points[i], points[j], points[k])){ cur++; } } if(cur + duplicate > max){ max = cur + duplicate; } cur = 1; } if(cur + duplicate > max){ max = cur; } cur = 1; } return max; } bool sameLine(Point p1, Point p2, Point p3){ return (p2.y - p3.y) * (p1.x - p3.x) == (p1.y - p3.y) * (p2.x - p3.x); }};
0 0
- Max Points on a Line | leetcode
- Max Points on a Line LEETCODE
- Leetcode: Max Points on a Line
- LeetCode - Max Points on a Line
- LeetCode 之 Max Points on a Line
- LeetCode | Max Points on a Line
- [LeetCode] Max Points on a Line
- Leetcode: Max Points on a Line
- LeetCode OJ:Max Points on a Line
- Leetcode Max Points on a Line
- Leetcode: Max Points on a Line .
- [Leetcode] Max Points on a Line (Java)
- leetcode:【Max Points on a Line】
- Max Points on a Line - LeetCode
- LeetCode:Max Points on a Line
- [LeetCode] Max Points on a Line
- leetcode - Max Points on a Line
- LeetCode(149)Max Points on a Line
- gcc编译
- Linux Shell 通配符、元字符、转义符使用实例介绍
- linux shell编程指南第十八章------控制流结构1
- Connect() 方式端口扫描 --python学习笔记
- 软件架构通用服务模式
- LeetCode | Max Points on a Line
- source insight 的一些快捷键,特殊功能,也想你所不知道的,转载自网络
- php数组函数
- 放置视图时,自动将模型文档iProperties拷贝到当前文档
- C/C++获取当前系统时间
- createPopup,右下角弹出提醒
- map.values()和map.keySet()的含义
- IOS中Json解析的四种方法
- oracle触发器中增删改查本表