leetcode 日经贴,Cpp code -Max Points on a Line
2015-04-27 16:14
302 查看
Max Points on a 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 ans = 0; double eps = 1e-6; int n = points.size(); if (n <= 2) return n; for (int i = 0; i < n; ++i) { vector<double> angles; int common = 0; for (int j = 0; j < n; ++j) { if (points[i].x == points[j].x && points[i].y == points[j].y) { common++; } else { double dx = points[j].x - points[i].x; double dy = points[j].y - points[i].y; angles.push_back(atan2(dy, dx)); } } sort(angles.begin(), angles.end()); int same = 0; for (int j = 0; j < angles.size(); ++j) { if (j == 0 || abs(angles[j] - angles[j - 1]) > eps) { ans = max(ans, same + common); same = 1; } else { ++same; } } ans = max(ans, same + common); } return ans; } };
相关文章推荐
- [LeetCode]Max Points on a Line
- [Leetcode]149. Max Points on a Line @python
- LeetCode | Max Points on a Line
- 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]149. 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
- Max Points on a Line --- LeetCode 面试题
- leetcode 149. Max Points on a Line
- 【LeetCode】-Max Points on a Line
- LeetCode:149_Max Points on a line | 寻找一条直线上最多点的数量 | Hard
- [leetcode] Max Points on a Line
- leetcode 149. Max Points on a Line
- [LeetCode] Max Points on a Line
- leetcode Max Points on a Line