leetcode149.直线上最多的点数
暴力枚举,固定一个点然后与后续的点相连,看看能有几个点在该直线上
直线上的点的个数用哈希表来存储
class Solution {public int maxPoints(int[][] points) {int result = 0;int n = points.length;if(n<3)return n;for (int i = 0; i < n; i++) {Map<Double, Integer> map = new HashMap<>();int currentMax = 0;for (int j = i + 1; j < n; j++) {double dy = points[j][1] - points[i][1];double dx = points[j][0] - points[i][0];Double k;if (dx == 0) {k = Double.POSITIVE_INFINITY;} else if (dy == 0) {k = 0.0;} else {k = dy / dx;}map.put(k, map.getOrDefault(k, 1) + 1);currentMax = Math.max(currentMax, map.get(k));}result = Math.max(result, currentMax);}return result;}
}