JAVA----------Easy Version To UnderStand!!!!!!!!


  • -2
    H
     public int maxPoints(Point[] points) {
        if (points == null || points.length == 0)
    		return 0;
    	if (points.length <= 2)
    		return points.length;
    	int max = 0;
    	for (int i = 0; i < points.length; i++) {
    		HashMap<Double, Integer> map = new HashMap<>();
    		int samePoint = 0, sameX = 0;
    		for (int j = 0; j < points.length; j++) {
    			if (i == j)
    				continue;
    			if (points[j].x == points[i].x && points[j].y == points[i].y){
    				samePoint++;
    				sameX++;
    				continue;
    			}
    			else 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 (map.get(k) == null)
    				map.put(k, 2);
    			else
    				map.put(k, map.get(k) + 1);
    		}
    
    		for (Double k : map.keySet()) {
    			max = Integer.max(max, map.get(k) + samePoint);
    		}
    		max = Integer.max(max, sameX+1);
    	}
    	return max;
    }

  • 0
    D

    The solution looks not complete?


  • 0
    H

    It is complete.It has been AC


  • 0
    J

    Er, the code you copied is not complete.


  • 0
    H

    ok.thank for your advice! I have completed it!!


  • 0
    B
    This post is deleted!

  • 0
    K

    Two parallel line have same slope but that case is not handled here.Why?

    Suppose, two points lie on first line and other two points lie on other line, so slope from 1st and 2nd will be same as from 3rd and 4th, but they are not on same line.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.