leetcode面试题3:Max Points on a Line
来源:互联网 发布:c语言输出心形图案 编辑:程序博客网 时间:2024/06/05 17:14
Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.
就是算在同一条直线上的点有多少个,需要注意的有几点:
1.坐标相同的点算在同一条直线上
2.斜率相同的点在同一条直线上,但特例是横坐标相同的点斜率不存在
思路是对每个点遍历剩余的点,创建一张哈希表map<double,int>,存储每个斜率对应的直线上的点数,斜率不存在时对应key值INT_MAX
AC代码:
class Solution {public: int maxPoints(vector<Point> &points) { double k; int i,j,max=0; if(points.size()<=2)return points.size(); for(i=0;i<points.size();i++) { map<double,int>count; count[INT_MIN]=0; int samepoint=1; for(j=0;j<points.size()&&j!=i;j++) { if(points[j].x==points[i].x&&points[j].y==points[i].y) samepoint++; else { double k=points[j].x==points[i].x?INT_MAX:(double)(points[j].y-points[i].y)/(double)(points[j].x-points[i].x); count[k]++; } } map<double,int>::iterator ite = count.begin(); for(ite;ite!=count.end();ite++) { if(ite->second+samepoint>max) max=ite->second+samepoint; } } return max; }};
0 0
- leetcode面试题3:Max Points on a Line
- Max Points on a Line --- LeetCode 面试题
- LeetCode 3 Max Points on a Line
- 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
- Unicode和UTF-8的关系
- 泛洪攻击的几种方法解析
- SSH面试题整理
- Remove Nth Node From End of List
- VIJOS P1908合唱队形
- leetcode面试题3:Max Points on a Line
- 集群中几种session同步解决方案的比较
- POJ 1704 Georgia and Bob(nim博弈论)
- 科学计算器(可实现带括号的浮点数四则运算)
- VIJOS P1777引水入城
- xmemcached的缓存更新机制
- zoj 2822 - Sum of Different Primes
- 基于套接字的编程
- 请假模块工作流分析(2)