문제
내가 작성한 정답
class Solution {
public int solution(int[][] dots) {
if(is(dots[0],dots[1],dots[2],dots[3])) return 1;
if(is(dots[0],dots[2],dots[1],dots[3])) return 1;
if(is(dots[0],dots[3],dots[1],dots[2])) return 1;
return 0;
}
private boolean is(int[] d1,int[] d2,int[] d3, int[] d4){
int n1 = d1[0]-d2[0];
int de1 = d1[1]-d2[1];
int n2 = d3[0]-d4[0];
int de2 = d3[1]-d4[1];
return n1*de2 == n2*de1;
}
}다른 사람들의 정답
class Solution {
int[][] dots;
public int solution(int[][] dots) {
this.dots = dots;
if (parallel(0, 1, 2, 3)) return 1;
if (parallel(0, 2, 1, 3)) return 1;
if (parallel(0, 3, 1, 2)) return 1;
return 0;
}
boolean parallel(int a, int b, int c, int d) {
int x = (dots[a][0] - dots[b][0]) * (dots[c][1] - dots[d][1]);
int y = (dots[a][1] - dots[b][1]) * (dots[c][0] - dots[d][0]);
return x == y || x == -y;
}
}Share article