Check If It Is a Straight Line
给一个数组, 里面是点, 问这些点在不在一个直线上. 这个题要用斜率公式算, y=ax+b, where (x,y) from array. 先用一组算出a, 然后再算b.
1 2 3 4 5 6 7 8 9 10 11 12 |
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; } } |