Problem Max Points On a Line
2014-06-29 13:47
232 查看
Problem Description:
Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.
Solution:
Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.
Solution:
public int maxPoints(Point[] points) { if (points.length <= 1) { return points.length; } Map<Double, Integer> map = new HashMap<Double, Integer>(); int maxPoints = 0; for (int i = 0; i < points.length; i++) { map.clear(); int samePointCount = 1; int max = 0; for (int j = i+1; j < points.length; j++) { if (points[i].x == points[j].x && points[i].y == points[j].y) { samePointCount++; } else { double slope = Double.MAX_VALUE; if (points[i].y == points[j].y) { slope = 0; } else if (points[i].x != points[j].x) slope = (points[i].y - points[j].y) / (double) (points[i].x - points[j].x); if (! map.containsKey(slope)) { map.put(slope, 1); } else { map.put(slope, map.get(slope)+1); } if (map.get(slope) > max) { max = map.get(slope); } } } if (max + samePointCount > maxPoints) { maxPoints = max + samePointCount ; } } return maxPoints; }
相关文章推荐
- LeetCode problem 3: Max Points on a Line
- Leetcode229: Max Points on a Line
- LeetCode 149. Max Points on a Line
- Max Points on a Line
- [LeetCode] Max Points on a Line
- leetcode 刷题之路 3 Max Points on a Line
- leetcode之Max Points on a Line
- 149. Max Points on a Line
- Max Points on a Line
- [Leetcode][python]Max Points on a Line
- Max Points on a Line
- max-points-on-a-line
- Max Points on a Line(转载,完全转载hackersun)
- LeetCode 之 Max Points on a Line
- [leetcode]Max Points on a Line @ Python
- Lintcode186 Max Points on a Line solution 题解
- 【leetcode】Max Points on a Line (python)
- LeetCode__Max Points on a Line
- LeetCode | 149. Max Points on a Line求多个点里面在一条直线上的点最多有多少个难题
- leetcode--max-points-on-a-line