algorithms

Algorithm implementations
git clone git://git.laack.co/algorithms.git
Log | Files | Refs | README

check-if-its-a-straight-line.cpp (840B)


      1 class Solution {
      2 public:
      3     bool checkStraightLine(vector<vector<int>>& coordinates) {
      4         // 2 <= coordinates.length <= 1000
      5         int dx = coordinates[0][0] - coordinates[1][0];
      6         int dy = coordinates[0][1] - coordinates[1][1];
      7 
      8         bool vertical = (dx == 0);
      9 
     10         for(auto& coord : coordinates) {
     11 
     12             int dxCheck = coord[0] - coordinates[0][0];
     13             int dyCheck = coord[1] - coordinates[0][1];
     14 
     15             if(vertical && dxCheck != 0) {
     16                 return false;
     17             } else if(vertical) {
     18                 continue;
     19             }
     20 
     21             if (dxCheck == 0 && dyCheck == 0) {
     22                 continue;
     23             }
     24 
     25             if(((double)dyCheck / (double)dxCheck != (double)dy / (double)dx)) {
     26                 return false;
     27             }
     28 
     29         }
     30 
     31         return true;
     32 
     33     }
     34 };