LeetCode 447. Number of Boomerangs; 149. Max Points on a Line

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode 447. Number of Boomerangs; 149. Max Points on a Line相关的知识,希望对你有一定的参考价值。

???????????????   hash   def   struct   tco   nbsp   table   ??????   ???????????????   

Boomerang ?????????????????????????????????????????? abc??????????????????ab,ac???????????????

??????????????????a????????????????????????a????????????????????????hashtable???????????????????????????????????????

?????????n?????????a?????????????????????????????? P_n^2 ????????? (????????????????????????????????????)

class Solution {
public:
    int numberOfBoomerangs(vector<pair<int, int>>& points) {
        int res=0;
        for (int i=0;i<points.size();++i){
            unordered_map<int,int> count; // <dis^2, numbers>
            for (int j=0;j<points.size();++j){
                if (i==j) continue;
                int dx=points[i].first-points[j].first;
                int dy=points[i].second-points[j].second;
                ++count[dx*dx+dy*dy];
            }
            for (auto i:count){ // P_n^2 since order matters
                int n=i.second;
                res += n*(n-1);
            }
        }
        return res;
    }
};

 

149. Max Points on a Line

??????????????????????????????????????????????????????????????????????????????????????????hashtable??????????????????????????????

??????j?????????i+1???????????????????????????????????????????????????x???????????????????????????????????????????????????????????????????????????????????????????????????????????????

?????????????????????????????????double????????????????????? <double,int> ??????hashtable ??????ac?????????????????? <dy/gcd, dx/gcd> ??????????????????????????????????????? ???x???????????????????????????????????????

?????????????????????????????????????????????

/**
 * 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) {
        int res=0;
        for (int i=0;i<points.size();++i){
            map<pair<int,int>,int> count; // <slope, num>
            int vertical=0;
            int depulicate=1; // one is for itself
            for (int j=i+1;j<points.size();++j){
                if (points[i].x==points[j].x && points[i].y==points[j].y) {++depulicate; continue;}
                //if (points[i].x==points[j].x) {++vertical; continue;} //can be omitted now
                int dy=points[i].y-points[j].y;
                int dx=points[i].x-points[j].x;
                int gcd=GCD(dx,dy);
                ++count[{dy/gcd, dx/gcd}];
            }
            for (auto x:count){
                res = max(res, x.second+depulicate);
            }
            //res = max(res,vertical+depulicate);
            res = max(res, depulicate);
        }
        return res;
    }
    
    int GCD(int a, int b){
        return b==0? a : GCD(b,a%b);
    }
};

 

以上是关于LeetCode 447. Number of Boomerangs; 149. Max Points on a Line的主要内容,如果未能解决你的问题,请参考以下文章

leetcode447-Number of Boomerangs

[LeetCode]447 Number of Boomerangs

leetcode 447 Number of Boomerangs

Leetcode 447. Number of Boomerangs JAVA语言

LeetCode 447. Number of Boomerangs; 149. Max Points on a Line

leetcode?pythonSum Of Two Number