zoukankan      html  css  js  c++  java
  • LeetCode OJ-- Max Points on a Line **@

    https://oj.leetcode.com/problems/max-points-on-a-line/

    给n多个二维平面上的点,求其中在同一条直线上点的最大数

    细节题,也挺考脑力的

    class Solution {
    public:
        int maxPoints(vector<Point> &points) {
            if(points.size() == 0 || points.size() == 1 || points.size() == 2)
                return points.size();
            
            map<double,int> lines; 
            int ans = 0;
            
            for(int i = 0; i< points.size(); i++)
            {
                lines.clear();//only use 斜率 represent a line, because from point i, they have a same point, so they are the same
                lines.insert(make_pair(INT_MIN,1)); //如果只有相同的点的时候,应该把点 i 算在内
                int dup = 0;
                
                for(int j = i + 1; j< points.size(); j++)
                {
                    if((points[i].x == points[j].x) && (points[i].y == points[j].y))
                    {
                        dup++; //record the same point with points[i], then every line + 1
                        continue;
                    }
                    //calc lines
                    double _k = ((points[i].x == points[j].x) ? INT_MAX : (points[i].y - points[j].y)*1.0/(points[i].x - points[j].x));
                    if(lines.count(_k)!=0)
                        lines[_k]++;
                    else
                        lines.insert(make_pair(_k,2));
                }
                for(map<double,int>::iterator itr = lines.begin(); itr != lines.end(); itr++)
                {
                    if(ans < dup + itr->second)
                        ans = dup + itr->second;
                }
            }
            return ans;
        }
    };
  • 相关阅读:
    使用a标签制作tooltips
    使用editorconfig配置你的编辑器
    JointJS绘制流程图
    用highcharts展现你的数据
    css段落首字母下沉
    sklearn框架的系统学习
    numpy删除二维数据矩阵的行和列
    sklearn中机器学习算法评价指标
    sklearn调用逻辑回归算法
    sklearn调用多项式回归
  • 原文地址:https://www.cnblogs.com/qingcheng/p/3870531.html
Copyright © 2011-2022 走看看