首页 > 其他 > 详细

Max Points on a Line

时间:2015-09-12 14:41:57      阅读:217      评论:0      收藏:0      [点我收藏+]

/**
 * 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)
    {
        unsigned int size = points.size();
       
        std::map<float, int> counter;
        int global_max = 0;
       
        for(int i = 0; i < size - 1; ++i)
        {
            k_infinity = 0;
            counter.clear();
           
            for(int j = i+1; j < size; ++j)
            {
                //check same point
                if(points[j].x == points[i].x && points[j].y == points[i].y)
                   continue;
               
                if(points[j].x == points[i].x)
                   k_infinity++
                  
                float k = (points[j].y - points[i].y) / (points[j].x - points[i].x);
                counter[k]++;
            }
           
            int local_max = 0;
            for(auto it = counter.begin(); it != counter.end(); ++it)
            {
                if(it->second > local_max)
                    local_max = it->second;
            }
           
            if(k_infinity > local_max)
                local_max = k_infinity;
           
           
            if(local_max > global_max)
                global_max = local_max;
           
        }
       
        return global_max;
    }
};

Max Points on a Line

原文:http://www.cnblogs.com/cis2000/p/4802895.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!