您的位置:首页 > 其它

Max Points on a Line 同一直线上的点

2014-08-23 23:39 399 查看
Max Points on a Line【question
comes from leetcode】

Given n points
on a 2D plane, find the maximum number of points that lie on the same straight line.


解题思路:

遍历point数组
选中一点,并求出其他点与该点的斜率,并求出其中的最大值
求出所有点中所求的最大值的最大值,即为所有结果

注意:

同一点的处理
位置相同的点的判断(不能用equal)
位置相同的点的处理

PS:

一开始我用的是maxPoints0方法,但是遇到第21组测试数据就不能通过了,我电脑(java version "1.7.0_25")上运行的结果是12,但是服务器运行的结果却是19,想了想,可能是有两个点的位置相同的时候就不能通过了,捣腾了好久也没解决,位置相同的点的处理实在太复杂。后来在网上看到用HashMap保留斜率和斜率相同的点数目的办法,的确简化了位置相同的点的处理,就借鉴过来了。哪位仁兄能帮我修改一下maxPoints0~

<span style="font-size:24px;">import java.awt.Point;
import java.util.HashMap;
import java.util.Map;
/**
 * 
 * @author Macro
 * @date 2014年8月23日
 */
public class Max_Points_on_a_Line {

    public static void main(String[] args) {
	Point[] points = new Point[21];
	// points[0] = new Point(0, 0);
	// for (int i = 0; i < 20; i++) {
	// points[i] = new Point(2, 2);
	// }
	// for (int i = 0; i < 10; i++) {
	// points[20 + i] = new Point(50, i);
	// }
	// for (int i = 0; i < 10; i++) {
	// points[30 + i] = new Point(90 + i, 90 + i);
	// }
//	points[0] = new Point(1, 1);
//	points[1] = new Point(1, 1);
//	points[2] = new Point(2, 2);
//	points[3] = new Point(2, 2);
//	points[4] = new Point(-30, -102);
	points[0] = new Point(0, 9);
	points[1] = new Point(138, 429);
	points[2] = new Point(115, 359);
	points[3] = new Point(115, 359);
	points[4] = new Point(-30, -102);
	points[5] = new Point(230, 709);
	points[6] = new Point(-150, -686);
	points[7] = new Point(-135, -613);
	points[8] = new Point(-60, -248);
	points[9] = new Point(-161, -481);
	points[10] = new Point(207, 639);
	points[11] = new Point(23, 79);
	points[12] = new Point(-230, -691);
	points[13] = new Point(-115, -341);
	points[14] = new Point(92, 289);
	points[15] = new Point(60, 336);
	points[16] = new Point(-105, -467);
	points[17] = new Point(135, 701);
	points[18] = new Point(-90, -394);
	points[19] = new Point(-184, -551);
	points[20] = new Point(150, 774);
	System.out.println(maxPoints0(points));
	
	System.out.println(maxPoints(points));

    }

    public static int maxPoints(Point[] points) {

	//special situation
	if (points.length < 2)
	    return points.length;

	int result = 0;// save the final result

	Map<Double, Integer> map = new HashMap<Double, Integer>();
	
	// deal with point i
	for (int i = 0; i < points.length; i++) {
	    int tempResult = 0;//save the temporary result
	    int sameVerticalPoints = 0;//save the counts of points witch in one vertical line
	    int samePoints = 0;// save the number of points witch in the same position as i
	    map.clear();

	    //deal with point j
	    for (int j = 0; j < points.length; j++) {
		if (i == j) {// itself
		    continue;
		} else {
		    if (points[i].x == points[j].x
			    && points[i].y == points[j].y) {// same position
			samePoints++;
			continue;
		    } else {
			if (points[i].x == points[j].x) {// vertical points
			    sameVerticalPoints++;
			    if (sameVerticalPoints > tempResult)
				tempResult = sameVerticalPoints;
			} else {// the slope of point i and j is exist
			    double slope = 0;
			    if (points[i].y == points[j].y)// horizontal points
				slope = 0;
			    else
				slope = ((double) (points[i].y - points[j].y))
					/ ((double) (points[i].x - points[j].x));

			    int temp = 1;// temporary variable
			    Double dslope = new Double(slope);
			    if (map.get(dslope) != null) {
				temp = map.get(dslope) + 1;
			    }
			    map.put(dslope, new Integer(temp));
			    if (temp > tempResult)
				tempResult = temp;
			}
		    }
		}
	    }//finish j
	    if (tempResult + samePoints > result)
		result = tempResult + samePoints;

	}// finish i

	return result + 1;
    

    }

    public static int maxPoints0(Point[] points) {

	int result = 0;// save the result
	int temp = 0;// save the temporary result
	int samePoint = 0;
	// point i
	for (int i = 0; i < points.length; i++) {
	    temp = 1;
	    samePoint = 0;
	    // point j
	    for (int j = i + 1; j < points.length; j++) {
		if (points[i].equals(points[j])) {
		    samePoint++;
		    continue;
		}
		temp = 2;

		// point k;
		for (int k = j + 1; k < points.length; k++) {
		    if (points[i].equals(points[k])
			    || points[j].equals(points[k])) {
			samePoint++;
			continue;
		    }
		    // if three points lie on the same straight line
		    if ((points[j].y - points[i].y)
			    * (points[k].x - points[i].x) == (points[k].y - points[i].y)
			    * (points[j].x - points[i].x)) {
			temp++;
		    }
		}

		// get the max result
		if (temp + samePoint > result) {
		    result = temp + samePoint;
		}

	    }
	    if (temp + samePoint > result) {
		result = temp + samePoint;
	    }

	}

	return result;

    }

}</span>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: