每日一题——1232. 缀点成线

1232. 缀点成线

分类:数学

终于来道会的了

class Solution {
public:
    bool checkStraightLine(vector<vector<int>>& coordinates) {
        int deltx = coordinates[1][0]-coordinates[0][0];
        int delty = coordinates[1][1]-coordinates[0][1];
        double k = (deltx == 0 ? INT_MAX : delty*1.0/deltx);
        for(int i=1; i<coordinates.size()-1; i++){
            int deltx2 = coordinates[i+1][0]-coordinates[i][0];
            int delty2 = coordinates[i+1][1]-coordinates[i][1];
            double k2 = (deltx2 == 0 ? INT_MAX : delty2*1.0/deltx2);
            if(k2 != k) return false;
        }
        return true;
    }
};

2021/01/17

posted @ 2021-01-17 10:36  vwmin  阅读(45)  评论(0)    收藏  举报