zoukankan      html  css  js  c++  java
  • 149.Max Points on a Line

    class Solution {
    public:
        int maxPoints(vector<Point>& points) {
            int res = 0;
            for (int i = 0; i < points.size(); ++i) {
                map<pair<int, int>, int> m;
                int duplicate = 1;
                for (int j = i + 1; j < points.size(); ++j) {
                    if (points[i].x == points[j].x && points[i].y == points[j].y) {
                        ++duplicate; continue;
                    } 
                    int dx = points[j].x - points[i].x;
                    int dy = points[j].y - points[i].y;
                    int d = gcd(dx, dy);
                    ++m[{dx / d, dy / d}];
                }
                res = max(res, duplicate);
                for (auto it = m.begin(); it != m.end(); ++it) {
                    res = max(res, it->second + duplicate);
                }
            }
            return res;
        }
        int gcd(int a, int b) {
            return (b == 0) ? a : gcd(b, a % b);
        }
    };
    
  • 相关阅读:
    PHP入门03 -- 数组与数据结构
    PHP入门02 -- 函数
    PHP入门01 -- 基本语法
    node文章
    Mongodb08
    Mongodb07
    ISO处理jq事件
    map
    Django自定义模板
    JS this指向
  • 原文地址:https://www.cnblogs.com/smallredness/p/10681983.html
Copyright © 2011-2022 走看看