One minute
Leetcode 1232
1232. Check If It Is a Straight Line
The idea of this solution is we use 3
points to find whether two slopes are equal. We can use the equation:
where
x0, x1, x2, y0, y1, y2
are all coordinates:
You might not understand the equation, so we can multiply divide both sides by
(x1 - x0)
and (x2 - x1)
and we can get:
The equation above is based off of a familar slope equation:
The reason we are doing the original equation instead of the where we have divided both sides by
(x1 - x0)
and (x2 - x1)
is we don’t have to worry about dividing by 0
such as when we divide both sides by (x1 - x0)
and (x2 - x1)
but (x1 - x0) = 0
or (x2 - x1) = 0
we have to contumplate what to do.
The Code:
Read other posts