Lint Code——最多共线的点的个数

本文介绍了一种解决给定点数组中找到共线点最多个数的问题的方法。使用时间复杂度为O(n^2)的算法,通过暴力求解方式,遍历每个点并利用map记录斜率对应的点数,最终找出共线点的最大数量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接:http://www.lintcode.com/zh-cn/problem/max-points-on-a-line/#

条件:给一个点数组

目标:求出共线的点的最多个数

实现:时间复杂度——O(n^2)

   要考虑的特殊情况是:①有相同点(这个也太特喵隐蔽了)②斜率不存在的点

思路:暴力求解,遍历每一个点,与他之后的点进行匹配,用一个map<double,int>存储斜率对应的个数。

代码:

/**
 * 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:
    /**
     * @param points an array of point
     * @return an integer
     */
    bool check(map<double,int> &res,double k){
        map<double ,int >::iterator it;
        it=res.find(k);
        if(it==res.end()){
            return false;
        }
        return true;
    }
    void change(map<double,int> &res,double k,int &num){
        
        map<double ,int >::iterator it;
        it=res.find(k);
        it->second++;
        if(it->second>num){
            num=it->second;
        }
    }

    int maxPoints(vector<Point>& points) {
        // Write your code here
        int num=0;
        if(points.size()==0){
            return num;
        }
        num=1;
        
        Point point_i,point_j;
        double k;
        
        for(int i=0;i<points.size();i++){
            point_i=points[i];
            int same=1;
            map<double,int> res;
            map<double ,int >::iterator it;
            for(int j=i+1;j<points.size();j++){
                point_j=points[j];
                if(point_j.x-point_i.x == 0 && point_j.y-point_i.y ==0 ){//同一点
                    same++;
                }else if(point_j.x-point_i.x == 0 && point_j.y-point_i.y !=0){
                    k=(numeric_limits<double>::max)();
                    if(check(res,k)){
                        it=res.find(k);
                        it->second++;
                    }else {
                        res.insert(pair<double,int>(k,1));
                    }
                }else if(point_j.x-point_i.x != 0 ){
                    k=(point_j.y-point_i.y)*1.0/(point_j.x-point_i.x);
                    if(check(res,k)){
                        it=res.find(k);
                        it->second++;
                    }else {
                        res.insert(pair<double,int>(k,1));
                    }
                }
            }
            if(res.empty()){
                if(same>num){
                    num=same;
                }
            }
            for(it=res.begin();it!=res.end();it++){
                if(it->second+same>num){
                    num=it->second+same;
                }
            }
        }
        return num;
    }
};
View Code

 

转载于:https://www.cnblogs.com/sylz/p/6181718.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值