LeetCode 149 — Max Points on a Line(C++ Java Python)
2014-03-02 10:50
841 查看
题目:http://oj.leetcode.com/problems/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.
题目翻译:
给定二维平面上的n个点,找出位于同一直线上的点的最大数目。
分析:
以点为中心,计算斜率,用map(python为dictionary)保存斜率与次数的映射,注意点重合以及斜率为无穷的情况。
C++实现:
Java实现:
Python实现:
感谢阅读,欢迎评论!
Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.
题目翻译:
给定二维平面上的n个点,找出位于同一直线上的点的最大数目。
分析:
以点为中心,计算斜率,用map(python为dictionary)保存斜率与次数的映射,注意点重合以及斜率为无穷的情况。
C++实现:
/** * 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 maxPoints = 0; int curPoints = 0; int samePoints = 0; double slope = 0; std::map<double, int> map; for(int i = 0; i < points.size(); ++i) { curPoints = 1; samePoints = 0; map.clear(); for(int j = i + 1; j < points.size(); ++j) { if(points[j].x == points[i].x && points[j].y == points[i].y) { ++samePoints; } else { if(points[j].x != points[i].x) { slope = 1.0 * (points[j].y - points[i].y) / (points[j].x - points[i].x); } else { slope = std::numeric_limits<double>::infinity(); } if(map.find(slope) == map.end()) { map[slope] = 2; } else { map[slope] += 1; } if(map[slope] > curPoints) { curPoints = map[slope]; } } } curPoints += samePoints; if(curPoints > maxPoints) { maxPoints = curPoints; } } return maxPoints; } };
Java实现:
/** * Definition for a point. * class Point { * int x; * int y; * Point() { x = 0; y = 0; } * Point(int a, int b) { x = a; y = b; } * } */ public class Solution { public int maxPoints(Point[] points) { int maxPoints = 0; int curPoints = 0; int samePoint = 0; double slope = 0; Map<Double, Integer> map = new HashMap<Double, Integer>(); for (int i = 0; i < points.length; ++i) { curPoints = 1; samePoint = 0; map.clear(); for (int j = i + 1; j < points.length; ++j) { if (points[j].x == points[i].x && points[j].y == points[i].y) { ++samePoint; } else { if (points[j].x != points[i].x) { slope = 1.0 * (points[j].y - points[i].y) / (points[j].x - points[i].x) + 0.0; // to avoid -0.0 } else { slope = Double.MAX_VALUE; } if (map.containsKey(slope)) { map.put(slope, map.get(slope) + 1); } else { map.put(slope, 2); } if (map.get(slope) > curPoints) { curPoints = map.get(slope); } } } curPoints += samePoint; if (curPoints > maxPoints) { maxPoints = curPoints; } } return maxPoints; } }
Python实现:
# Definition for a point # class Point: # def __init__(self, a=0, b=0): # self.x = a # self.y = b class Solution: # @param points, a list of Points # @return an integer def maxPoints(self, points): if len(points) < 3: return len(points) maxPoints = 0 i = 0 while i < len(points): curPoints = 1 samePoints = 0 dict = {} j = i + 1; while j < len(points): if points[j].x == points[i].x and points[j].y == points[i].y: samePoints += 1 else: if points[j].x != points[i].x: slope = 1.0 * (points[j].y - points[i].y) / (points[j].x - points[i].x) else: slope = float('Inf') if slope in dict: dict[slope] += 1 else: dict[slope] = 2 if dict[slope] > curPoints: curPoints = dict[slope] j += 1 curPoints += samePoints if curPoints > maxPoints: maxPoints = curPoints i += 1 return maxPoints
感谢阅读,欢迎评论!
相关文章推荐
- leetcode 149:Max Points on a Line(java实现)细致分析
- leetcode_c++:哈希:Max Points on a Line(149)
- Leetcode_max-points-on-a-line(c++ and python version)
- [Leetcode]149. Max Points on a Line @python
- [LeetCode]题解(python):149-Max Points on a Line
- [leetcode-149]Max Points on a Line(java)
- Java for LeetCode 149 Max Points on a Line
- [leetcode] 149. Max Points on a Line 解题报告
- leetcode 149 —— Max Points on a Line
- LeetCode | 149. Max Points on a Line求多个点里面在一条直线上的点最多有多少个难题
- LeetCode 149 Max Points on a Line
- LeetCode-Max Points on a Line (Python)
- Leetcode#149 Max Points on a Line
- Leetcode 149-Max Points on a Line
- LeetCode149—Max Points on a Line
- leetcode 149. Max Points on a Line
- [LeetCode] 149. Max Points on a Line 共线点个数
- [LeetCode]149. Max Points on a Line 深入浅出讲解和代码示例
- LeetCode 149. Max Points on a Line
- leetcode题解-149. Max Points on a Line