leetcode_Max Points on a Line

    /*
     * Max Points on a Line 
     * */
    public int maxPoints(Point[] points) 
    {
    	int max = 0;
    	for(int i=0;i<points.length-1;i++)
    	{
    		for(int j=i+1;j<points.length;j++)
    		{
    			int num=0;
    			
    			//if this 2 points are the same , skip this time, or we'll get result 'n' which is probably not correct!
    			if(points[j].x == points[i].x && points[j].y == points[i].y)
    			{
    				continue;
    			}
    				
    			
    			for(int k=0;k<points.length;k++)
    			{
    				int isLine = (points[i].y-points[k].y)*(points[j].x-points[k].x)-(points[j].y-points[k].y)*(points[i].x-points[k].x);
    				if(isLine==0)
    				{
    					num++;
    				}
    			}
    			if(num>max)
	    		{
	    			max = num;
	    		}
    		}
    		
    	}
    	
    	//if only one or two points, above judgement will get result 0  while the correct result is 1 or 2
    	if(points.length<=2)
    	{
    		max = points.length;
    	}
    	else if(max==0)
    	{
    		// this case is that all points are the same
    		max=points.length;
    	}
		return max;
    }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章