Description


Submission
class Solution {
public:
bool checkStraightLine(vector<vector<int>>& coordinates) {
int x0 = coordinates[0][0];
int y0 = coordinates[0][1];
for(auto& v: coordinates) {
v[0] -= x0;
v[1] -= y0;
}
int a = -coordinates[1][1];
int b = coordinates[1][0];
for(auto& p: coordinates) {
if(a * p[0] + b * p[1] != 0) return false;
}
return true;
}
};
// move all the nodes so that he line passes origin.
// to make it passing the origin, we move one point to the origin.
// (just taking the first one!)
// After that, the line equation will be ax + by = 0
// The we've got an equation
// ax0 + by0 = 0
// ax1 + by1 = 0
// a b
// a b
// By solving the equation,
// a = -y0, b = x0
class Solution {
public:
bool checkStraightLine(vector<vector<int>>& coordinates) {
int x0 = coordinates[0][0];
int y0 = coordinates[0][1];
for(auto& v: coordinates) {
v[0] -= x0;
v[1] -= y0;
}
int a = -coordinates[1][1];
int b = coordinates[1][0];
for(auto& p: coordinates) {
if(a * p[0] + b * p[1] != 0) return false;
}
return true;
}
};
// move all the nodes so that he line passes origin.
// to make it passing the origin, we move one point to the origin.
// (just taking the first one!)
// After that, the line equation will be ax + by = 0
// The we've got an equation
// ax0 + by0 = 0
// ax1 + by1 = 0
// a b
// a b
// By solving the equation,
// a = -y0, b = x0
class Solution { public: bool checkStraightLine(vector<vector<int>>& coordinates) { int x0 = coordinates[0][0]; int y0 = coordinates[0][1]; for(auto& v: coordinates) { v[0] -= x0; v[1] -= y0; } int a = -coordinates[1][1]; int b = coordinates[1][0]; for(auto& p: coordinates) { if(a * p[0] + b * p[1] != 0) return false; } return true; } }; // move all the nodes so that he line passes origin. // to make it passing the origin, we move one point to the origin. // (just taking the first one!) // After that, the line equation will be ax + by = 0 // The we've got an equation // ax0 + by0 = 0 // ax1 + by1 = 0 // a b // a b // By solving the equation, // a = -y0, b = x0
