LeetCode----Max Points On a Line

2014-11-24 09:00:30 · 作者: · 浏览: 0

Max Points on a Line

Total Accepted: 4246 Total Submissions: 42602

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


分析: 任取两点组成的直线,看有多少点在其上面。

注意: 考虑 3 种情况: 两点重合; 斜率为无穷大; 正常情况

/**
 * 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
  
    &points) {
        int max_points(0);
        double a(0), b(0); // denote a straight line
        LINE_TYPE line_type(LINE); 
        
        if(points.size() == 0) return 0;
        else if(points.size() == 1) return 1;
        
        for(int i=0; i
   
    (points[j].x - points[i].x); //tangent b = points[i].y - a * points[i].x; } // compute how many points on the line int point_count(2); for(int k=0; k