1232.缀点成线
思路一:直线方程
class Solution {
public boolean checkStraightLine(int[][] coordinates) {
// 因为点的数量一定大于等于2,先用前两个点算斜率
int x0=coordinates[0][0],x1=coordinates[1][0];
int y0=coordinates[0][1],y1=coordinates[1][1];
// 如果是垂直的一条直线
if(x0==x1){
for(int i=2;i<coordinates.length;i++)
if(coordinates[i][0]!=x0)
return false;
}
else{
double k = (double)(y1-y0)/(x1-x0);
for(int i=2;i<coordinates.length;i++)
if(coordinates[i][1]!=(int)(k*(coordinates[i][0]-x0)+y0))
return false;
}
return true;
}
}
思路二:
( x 2 − x 1 ) ∗ ( y 3 − y 2 ) = = ( y 2 − y 1 ) ∗ ( x 3 − x 2 ) (x2 - x1) * (y3 - y2) == (y2 - y1) * (x3 - x2) (x2−x1)∗(y3−y2)==(y2−y1)∗(x3−x2)