LeetCode 149 Max Points on a Line

2019-05-10 10:22:30 浏览数 (1)

题目

煞笔题目,怪不得那么多人diss

两层for循环,它还疯狂卡精度,double的精度不够,必须long double

代码语言:javascript复制
class Solution {
public:
    map<pair<long double,long double>,int> m;
    map<pair<long double,long double>,int> m2;
    map<pair<pair<long double,long double>,int>,int> m3;
    
    int maxPoints(vector<vector<int>>& points) {
        
        if(points.size()==0)
            return 0;
        int mm =1;
        
        for(int i=0;i<points.size();i  )
        {
            
            m2[make_pair(points[i][0],points[i][1])]  ;
            
            mm =max(mm,m2[make_pair(points[i][0],points[i][1])]);
            
        }
        
        for(int i=0;i<points.size();i  )
        {
            for(int j=i 1;j<points.size();j  )
            {
                
                long double k;
                if(points[i][0]-points[j][0]==0)
                    k = 99999999;
                else
                    k =(long double) (points[i][1] - points[j][1]) / (points[i][0] - points[j][0]);
                long double b;
                if(points[i][0]-points[j][0]==0)
                    b = points[i][0];
                else
                    b = points[i][1] - points[i][0]*k;
                
                //k = (long double)(((long long int)(k*10000000000))/10000000000.0);
                //b = (long double)(((long long int)(b*10000000000))/10000000000.0);
            
                if(m3[make_pair(make_pair(k,b),i)]==0)
                {
                    m[make_pair(k,b)]  =1;
                    m3[make_pair(make_pair(k,b),i)]=1;
                }
                
                if(m3[make_pair(make_pair(k,b),j)]==0)
                {
                    m[make_pair(k,b)]  =1;
                    m3[make_pair(make_pair(k,b),j)]=1;
                }
                
                
            }
        }
        
        map<pair<long double,long double>,int>::iterator it;
        
        it = m.begin();
        
        while(it != m.end())
        {
            
            mm =max(mm,it->second);
            it  ;
        }
        return mm;
        
    }
};

0 人点赞