What does input:[] mean? null maybe?


  • 0
    B

    In my local Eclipse environment. It works good. If the input is null, it can return 0;

    But in OJ, i says like following. Anybody knows what does input [] mean? Thank you all.

    22 / 27 test cases passed.
    Status: Wrong Answer
    Submitted: 0 minutes ago
    Input:	[]
    Output:	2
    Expected:	0
    

    here is my java code:

    public static int maxPoints(Point[] points) {
    	if (points == null) {
    		return 0;
    	} else if (points.length == 1) {
    		return 1;
    	} else if (points.length == 2) {
    		return 2;
    	}
    	/**
    	 * <slope,count>
    	 */
    	Map<Double, Integer> slopeMap = new HashMap<Double, Integer>();
    	long infinite = Integer.MAX_VALUE;
    	double slope;
    	int maxLines = 1;
    	int count = 0;
    	for (int i = 0; i < points.length - 1; i++) {
    		boolean b = false;
    		slopeMap.put(0.0, 1);
    		for (int j = i + 1; j < points.length; j++) {
    			if (points[i].equals(points[j])) {
    				if (slopeMap.containsKey(0.0)) {
    					slopeMap.put(0.0, 1);
    				}
    				continue;
    			}
    
    			if (points[i].x == points[j].x) {
    				slope = infinite;
    			} else {
    				slope = 1.0 * (points[j].y - points[i].y) / (points[j].x - points[i].x);
    			}
    			if (!slopeMap.containsKey(slope)) {
    				count = 2;
    			} else {
    				count = slopeMap.get(slope) + 1;
    			}
    			slopeMap.put(slope, count);
    			if (count > maxLines) {
    				maxLines = count;
    				b = true;
    			}
    		}
    		if (b)
    			maxLines = maxLines + slopeMap.get(0.0);
    		else if (slopeMap.get(0.0) > maxLines)
    			maxLines = slopeMap.get(0.0);
    	}
    	/**
    	 *  maxLines = Combination(maxPoints,2)
    	 *  so maxPoints = sqrt(2*maxLines) + 1
    	 */
    	return (int) (Math.sqrt(2 * maxLines) + 1);
    }

  • 1
    M

    [] means the empty set, an array or list with a length of zero. The array isn't null, as suggested by your first if, it just has no elements stored in it. In this case, there are no points in the array. Since there are no points, the maximum number of points on a line is zero.


  • 0
    B

    Thank you very much. You are right. I overlooked this case.


  • 0
    B

    please ignore the code above.


Log in to reply
 

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