# Hard-题目53：149. 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.

public class Solution {
public int maxPoints(Point[] points) {
if(points.length <= 0) return 0;
if(points.length <= 2) return points.length;
int result = 0;
for(int i = 0; i < points.length; i++){
HashMap<Double, Integer> hm = new HashMap<Double, Integer>();
int samex = 1;
int samep = 0;
for(int j = 0; j < points.length; j++){
if(j != i){
if((points[j].x == points[i].x) && (points[j].y == points[i].y)){
samep++;
}
if(points[j].x == points[i].x){
samex++;
continue;
}
double k = (double)(points[j].y - points[i].y) / (double)(points[j].x - points[i].x);
if(hm.containsKey(k)){
hm.put(k,hm.get(k) + 1);
}else{
hm.put(k, 2);
}
result = Math.max(result, hm.get(k) + samep);
}
}
result = Math.max(result, samex);
}
return result;
}
}

46ms,17.98%,27ms,7.06%
cmershen的碎碎念：

#### LeetCode 149 — Max Points on a Line（C++ Java Python）

2014-03-02 10:50:24

#### LeetCode(149)Max Points on a Line

2014-03-07 13:49:08

#### Leetcode 149. Max Points on a Line

2016-04-29 10:38:12

#### 【LeetCode】Max Points on a Line 解题报告

2014-08-28 22:05:38

#### Max Points on a Line -- LeetCode

2014-03-14 00:45:59

#### 【LeetCode with Python】 Max Points on a Line

2014-09-21 17:41:21

#### [leetcode]149. Max Points on a Line@Java解题报告

2017-08-01 13:42:39

#### LeetCode Max Points on a Line　解题报告

2014-01-30 14:42:31

#### Leetcode: Max Points on a Line

2013-12-03 22:57:09

#### [LeetCode]149. Max Points on a Line 深入浅出讲解和代码示例

2017-07-13 14:23:34