Valid Boomerang
检查是否三点共线,是返回false,不是返回true。我看答案有几个用的是三角形面积的解法。我觉得那种解法更巧妙, 我用的是直接的y/x来判断三点共线,注意了一下x为0和三点重合的情况。重合的时候直接false。
1 2 3 4 5 6 7 8 9 10 11 12 |
class Solution { public boolean isBoomerang(int[][] points) { if(check(points[0],points[1]) || check(points[0],points[2]) || check(points[1],points[2])) return false; double x = (double)(points[1][1] - points[0][1]) / (points[1][0] - points[0][0]); double y = (double)(points[2][1] - points[1][1]) / (points[2][0] - points[1][0]); return x != y; } private boolean check(int[] x, int[] y) { return x[0] == y[0] && x[1] == y[1]; } } |