leetcode--max-points-on-a-line
2018-02-11 18:43
411 查看
描述
Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.代码
/** * Definition for a point. * class Point { * int x; * int y; * Point() { x = 0; y = 0; } * Point(int a, int b) { x = a; y = b; } * } */ import java.util.*; public class Solution { public int maxPoints(Point[] points) { if(points.length==0){ return 0; } if(points.length==1){ return 1; } int max=0; for(int i=0;i<points.length;i++){ Map<Double,Integer> map=new HashMap<>(); Map<Point,Integer> exi=new HashMap<>(); exi.put(points[i],1); for(int j=0;j<points.length;j++){ if(i!=j){ Integer temp=exi.get(points[j]); if(points[i].y==points[j].y&&points[i].x==points[j].x){ Integer count=exi.get(points[i]); exi.put(points[i],count+1); if(exi.get(points[i])>max){ max=exi.get(points[i]); } }else{ double k=(double)(points[i].y-points[j].y)/(points[i].x-points[j].x); if(map.get(k)==null){ if(exi.get(points[i])!=null){ map.put(k,1+exi.get(points[i])); }else{ map.put(k,2); } }else{ int v=map.get(k); map.put(k,++v); } if(map.get(k)>max){ max=map.get(k); } } } } } return max; } }
相关文章推荐
- 125_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-[3]Max Points on a Line
- Leetcode: Max Points on a Line
- LeetCode-Max Points on a Line-最大共线的点的数目-Hash
- Leetcode Max Points on a Line
- LeetCode--max-points-on-a-line
- LeetCode 149. Max Points on a Line
- LeetCode-Max Points on a Line[AC源码]
- [leetcode]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】Max Points on a Line
- leetcode 149: Max Points on a Line
- [Leetcode][python]Max Points on a Line
- [Leetcode]149. Max Points on a Line @python
- Leetcode 149. Max Points on a Line