LeetCode - Max Points on a Line
2013-12-10 15:00
417 查看
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.
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 result = 0; for (int i = 0; i < points.size(); ++i) { std::unordered_map<string, int> number; int mx = 0; int same = 1; for (int j = i+1; j < points.size(); ++j) { int x = points[j].x - points[i].x; int y = points[j].y - points[i].y; int g = gcd(x, y); if (g !=0) { x /= g; y /= g; mx = max(mx, ++number[to_string(x) + " " + to_string(y)]); } else { same++; continue; } } result = max(result, (mx + same)); } return result; } int gcd(int a, int b) { return a? a/abs(a)*abs(gcd(b%a, a)) : b; } };
相关文章推荐
- leetcode: Max Points on a Line
- LeetCode-Max Points on a Line[AC源码]
- leetcode -- Max Points on a Line -- 重点
- 【LeetCode】Max Points on a Line 解题报告
- leetcode - Max Points on a Line
- 125_leetcode_Max Points on a Line
- #leetcode#Max Points on a Line
- LeetCode: Max Points on a Line
- leetcode-[3]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-最大共线的点的数目-Hash
- [leetCode]:Max Points on a Line
- 【九度】题目1548:平面上的点 && 【LeetCode】Max Points on a Line
- leetcode 149. Max Points on a Line
- Leetcode 149. Max Points on a Line (Hard) (cpp)
- LeetCode149. Max Points on a Line
- Max Points on a Line -- LeetCode
- LeetCode:max-points-on-a-line