Check If It Is a Straight Line
给一个数组, 里面是点, 问这些点在不在一个直线上. 这个题要用斜率公式算, y=ax+b, where (x,y) from array. 先用一组算出a, 然后再算b.
class Solution {
public boolean checkStraightLine(int[][] c) {
double s = (double)(c[1][1] - c[0][1]) / (c[1][0] - c[0][0]);
double m = c[0][1] - s*c[0][0];
for(int i = 2; i < c.length; i++) {
int t = (int)(s * c[i][0] + m);
if(t != c[i][1])
return false;
}
return true;
}
}