점 네 개의 좌표를 담은 이차원 배열 dots가 다음과 같이 매개변수로 주어집니다.
주어진 네 개의 점을 두 개씩 이었을 때, 두 직선이 평행이 되는 경우가 있으면 1을 없으면 0을 return 하도록 solution 함수를 완성해보세요.
/**
* @param {int[][]} dots
* @return {int}
*/
class Solution1 {
public int solution(int[][] dots) {
float[] distances = new float[2];
float[] temp = new float[2];
for (int i = 1; i <= 3; i++) {
int cnt = 0;
for (int j = 1; j < dots.length; j++) {
if (i == j) {
distances[0] = (float) (dots[0][0] - dots[j][0]) / (dots[0][1] - dots[j][1]);
} else {
if (cnt == 0) {
temp[0] = dots[j][0];
temp[1] = dots[j][1];
cnt++;
continue;
}
distances[1] = (float) (temp[0] - dots[j][0]) / (temp[1] - dots[j][1]);
}
}
if (distances[0] == distances[1]) {
return 1;
}
}
return 0;
}
}
class Solution2 {
public int solution(int[][] dots) {
int[][] matchs = new int[][] { { 0, 1, 2, 3 }, { 0, 2, 1, 3 }, { 0, 3, 1, 2 } };
for (int[] match : matchs) {
if (gradient(dots[match[0]], dots[match[1]]) == gradient(dots[match[2]], dots[match[3]])) {
return 1;
}
}
return 0;
}
public float gradient(int[] a, int[] b) {
float top = a[0] - b[0];
float bot = a[1] - b[1];
return top / bot;
}
}
/ by zero 예외가 발생하지 않는다.divisor != 0 인 경우로 처리하자.