LeetCode 149 — Max Points on a Line(C++ Java Python)

题目:http://oj.leetcode.com/problems/max-points-on-a-line/

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

题目翻译:

给定二维平面上的n个点,找出位于同一直线上的点的最大数目。
分析:

        以点为中心,计算斜率,用map(python为dictionary)保存斜率与次数的映射,注意点重合以及斜率为无穷的情况。

C++实现:

/**
 * Definition for a point.
 * struct Point {
 *     int x;
 *     int y;
 *     Point() : x(0), y(0) {}
 *     Point(int a, int b) : x(a), y(b) {}
 * };
 */
class Solution {
public:
    int maxPoints(vector<Point> &points) {
    	int maxPoints = 0;
    	int curPoints = 0;
    	int samePoints = 0;
    	double slope = 0;
    	std::map<double, int> map;

    	for(int i = 0; i < points.size(); ++i)
    	{
    		curPoints = 1;
    		samePoints = 0;
    		map.clear();

    		for(int j = i + 1; j < points.size(); ++j)
    		{
    			if(points[j].x == points[i].x && points[j].y == points[i].y)
				{
    				++samePoints;
				}
    			else
    			{
    				if(points[j].x != points[i].x)
    				{
    					slope = 1.0 * (points[j].y - points[i].y) / (points[j].x - points[i].x);
    				}
    				else
    				{
    					slope = std::numeric_limits<double>::infinity();
    				}

    				if(map.find(slope) == map.end())
    				{
    					map[slope] = 2;
    				}
    				else
    				{
    					map[slope] += 1;
    				}

    				if(map[slope] > curPoints)
    				{
    					curPoints = map[slope];
    				}
    			}
    		}

    		curPoints += samePoints;

    		if(curPoints > maxPoints)
    		{
    			maxPoints = curPoints;
    		}
    	}

    	return maxPoints;
    }
};

Java实现:

/**
 * Definition for a point.
 * class Point {
 *     int x;
 *     int y;
 *     Point() { x = 0; y = 0; }
 *     Point(int a, int b) { x = a; y = b; }
 * }
 */
public class Solution {
    public int maxPoints(Point[] points) {
		int maxPoints = 0;
		int curPoints = 0;
		int samePoint = 0;
		double slope = 0;
		Map<Double, Integer> map = new HashMap<Double, Integer>();

		for (int i = 0; i < points.length; ++i) {
			curPoints = 1;
			samePoint = 0;
			map.clear();
			
			for (int j = i + 1; j < points.length; ++j) { 	
				if (points[j].x == points[i].x && points[j].y == points[i].y) {
					++samePoint;
				} else {
					if (points[j].x != points[i].x) {
						slope = 1.0 * (points[j].y - points[i].y) / (points[j].x - points[i].x) + 0.0;  // to avoid -0.0  
					} else {
						slope = Double.MAX_VALUE;
					}
					
					if (map.containsKey(slope)) {
						map.put(slope, map.get(slope) + 1);
					} else {
						map.put(slope, 2);
					}
					
					if (map.get(slope) > curPoints) {
						curPoints = map.get(slope);
					}
				}
			}

			curPoints += samePoint;

			if (curPoints > maxPoints) {
				maxPoints = curPoints;
			}
		}

		return maxPoints;
    }
}

Python实现:

# Definition for a point
# class Point:
#     def __init__(self, a=0, b=0):
#         self.x = a
#         self.y = b

class Solution:
    # @param points, a list of Points
    # @return an integer
    def maxPoints(self, points):
        if len(points) < 3:
            return len(points)
        
        maxPoints = 0
        
        i = 0
        while i < len(points):
            curPoints = 1
            samePoints = 0
            dict = {}
            
            j =  i + 1;    
            while j < len(points):
                if points[j].x == points[i].x and points[j].y == points[i].y:
                    samePoints += 1
                else:
                    if points[j].x != points[i].x:
                        slope = 1.0 * (points[j].y - points[i].y) / (points[j].x - points[i].x)
                    else:
                        slope = float('Inf')
                        
                    if slope in dict:
                        dict[slope] += 1
                    else:
                        dict[slope] = 2
                        
                    if dict[slope] > curPoints:
                        curPoints = dict[slope]
                
                j += 1
            
            curPoints += samePoints
            
            if curPoints > maxPoints:
                maxPoints = curPoints
            
            i += 1
            
        return maxPoints

        感谢阅读,欢迎评论!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值