LeetCode: Max Points on a Line
来源:互联网 发布:java list排序算法 编辑:程序博客网 时间:2024/05/20 00:17
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) { int max = 0; if(points.empty()) return 0; for(int i = 0; i < points.size(); i++) { map<float, int> slopeMap; int numOfEqual = 0, numOfSamePoint = 0; float slope = 0; int slopeArray[1000]; for(int j = 0; j < points.size(); j++) { if(i == j) continue; if(points[i].x == points[j].x && points[i].y != points[j].y) { numOfEqual++; if(numOfEqual > max) max = numOfEqual; } else if(points[i].x == points[j].x && points[i].y == points[j].y) { numOfSamePoint++; if(numOfSamePoint > max) max = numOfSamePoint; } else { slope = (float)(points[i].y - points[j].y)/(float)(points[i].x - points[j].x); if(++slopeMap[slope] + numOfSamePoint> max) max = slopeMap[slope] + numOfSamePoint; } } } return ++max; }};
Round 2:
/** * 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.empty()) return 0; int max = 0; for(int i = 0; i < points.size(); i++) { int numOfSamePoint = 0, numOfSameXPoint = 0; unordered_map<float, int> m_map; for(int j = 0; j < points.size(); j++) { if(i == j) continue; if(points[i].x == points[j].x && points[i].y == points[j].y) { numOfSamePoint++; if(numOfSamePoint > max) max = numOfSamePoint; } else if(points[i].x == points[j].x && points[i].y != points[j].y) { numOfSameXPoint++; if(numOfSameXPoint > max) max = numOfSameXPoint; } else { float slope = (float)(points[j].y - points[i].y)/(float)(points[j].x - points[i].x); m_map[slope]++; if(m_map[slope] + numOfSamePoint > max) max = m_map[slope] + numOfSamePoint; } } } return max+1; }};
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
- C++实现开机启动
- HDU 1082
- size_type与size_t的机器无关性
- poj 3320 Jessica's Reading Problem
- POJ 1006 Biorhythms
- LeetCode: Max Points on a Line
- 树链剖分-链的剖分(线段树维护+离线操作)
- 关于ios越狱开发的那些事
- 7518爱的是看了就关联方看见过开了房7518季后赛的规范健康
- 微软MSDN订阅介绍
- cmake函数参数解析
- VC 调用 VB 的DLL
- 老罗锤子手机发布会,我感到深深地愧疚!
- uva 10453 - Make Palindrome