LeetCode | Max Points on a Line
2014-05-04 20:57
513 查看
原题描述:https://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.
解题思路:
1. 依次遍历数组中的每个点,以当前点为基准,再依次遍历其后的每个点(注:不是从头开始遍历,这样可以避免重复计算),每遍历到一个点,可以确定一条直线,然后用一个哈希表存储并维护该直线(key)及其上点数(value),同时我们可以维护一个最大点数值max,在每次遍历结束后,通过比较来刷新最大值。
2. 那么,怎样来存储一条直线呢?斜率!一般我们的做法是根据两个点的坐标计算出double类型的斜率,然后进行存储。但是考虑到double类型也是会丢失精度的,我决定用String类型来存储斜率,这样虽然不如double类型方便,但是能更好地保证程序的可靠性。String类型存储斜率时,需要注意对分数形式的斜率进行化简,以及对零值、负数的处理。
3. 两种特殊情况:a)数组中有重复的点;b)直线与 x轴 或者 y轴平行。
实现代码:
Given n points
on a 2D plane, find the maximum number of points that lie on the same straight line.
解题思路:
1. 依次遍历数组中的每个点,以当前点为基准,再依次遍历其后的每个点(注:不是从头开始遍历,这样可以避免重复计算),每遍历到一个点,可以确定一条直线,然后用一个哈希表存储并维护该直线(key)及其上点数(value),同时我们可以维护一个最大点数值max,在每次遍历结束后,通过比较来刷新最大值。
2. 那么,怎样来存储一条直线呢?斜率!一般我们的做法是根据两个点的坐标计算出double类型的斜率,然后进行存储。但是考虑到double类型也是会丢失精度的,我决定用String类型来存储斜率,这样虽然不如double类型方便,但是能更好地保证程序的可靠性。String类型存储斜率时,需要注意对分数形式的斜率进行化简,以及对零值、负数的处理。
3. 两种特殊情况:a)数组中有重复的点;b)直线与 x轴 或者 y轴平行。
实现代码:
/** * 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 { //求最大公约数. gcd(0,3)=3, gcd(0,0)=0 private static int gcd(int a, int b) { return (b!=0) ? gcd(b, a % b) : a; } public int maxPoints(Point[] points) { if (points == null) return 0; int len = points.length; if (len <= 2) return len; HashMap<String, Integer> records = new HashMap<>(); //key为斜率,value为对应直线上点的个数 int result = 0; Point pa, pb; int x, y; for (int i = 0; i < len - 1; i++) { records.clear(); //每次遍历,清空哈希表 int same = 1; //记录重复点的个数 int max = 0; pa = points[i]; for (int j = i + 1; j < len; j++) { pb = points[j]; //确保x值为非负数 if (pa.x >= pb.x) { x = pa.x - pb.x; y = pa.y - pb.y; } else { x = pb.x - pa.x; y = pb.y - pa.y; } int gcdXY = gcd(Math.abs(x), Math.abs(y)); if (gcdXY == 0) { ++same; } else { x /= gcdXY; y /= gcdXY; if (x == 0 || y == 0) { //直线平行x轴或者y轴 x = Math.abs(x); y = Math.abs(y); } String key = x + "/" + y; //用String类型存储斜率,需要将其最简化 int value = 0; if (records.containsKey(key)) { value = records.get(key); } records.put(key, ++value); max = Math.max(max, value); } } result = Math.max(result, max+same); } return result; } }
相关文章推荐
- [leetcode] Max Points on a Line
- [LeetCode]Max Points on a Line
- leetcode 日经贴,Cpp code -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 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: Max Points on a Line
- leetcode3题解 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
- leetcode 149. Max Points on a Line
- Leetcode 149. Max Points on a Line (Hard) (cpp)
- leetcode: Max Points on a Line
- LeetCode-Max Points on a Line[AC源码]