LeetCode 149 — Max Points on a Line(C++ Java Python)
来源:互联网 发布:物流系统源码 编辑:程序博客网 时间:2024/05/09 22:13
题目: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++实现:
/** * 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 = bclass 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
感谢阅读,欢迎评论!
0 0
- LeetCode 149 — Max Points on a Line(C++ Java Python)
- LeetCode-Max Points on a Line (Python)
- 【leetcode】Max Points on a Line (python)
- Max Points on a Line Leetcode Python
- [Leetcode][python]Max Points on a Line
- [Leetcode] Max Points on a Line (Java)
- LeetCode:Max Points on a Line (Java)
- LeetCode(149)Max Points on a Line
- LeetCode: Max Points on a Line [149]
- 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
- (leetcode)Max Points on a Line
- [leetcode-149]Max Points on a Line(java)
- leetcode 149 —— Max Points on a Line
- LeetCode(149) Max Points on a Line
- php持久登录、记住我功能实现
- IOS语言Object-c语言随笔(二)
- android 拍照代码
- 读书笔记——深入理解java虚拟机第2章(java内存区域与内存溢出异常)
- Myeclipse6.5中修改servlet JSP等模板的方法
- LeetCode 149 — Max Points on a Line(C++ Java Python)
- 字符设备驱动程序之poll机制
- hdu 4223 Dynamic Programming?(最小连续子序列绝对值和)
- 操作系统(4)线程及其实现
- Unity开发者总结的5点工作经验
- MongoDB_03之使用shell命令实现对文档数据的操作
- int \float\char等基本数据类型所占的字节数
- Cocos2d-x JSB 自动绑定bindings
- struts.properties详解