코린이의 소소한 공부노트

[LeetCode/Easy] 1037. Valid Boomerang 본문

코딩테스트 풀이/JAVA

[LeetCode/Easy] 1037. Valid Boomerang

무지맘 2023. 4. 4. 22:20

1. Input

1) int[][] points

- 세 점의 좌표를 담고 있다.

 

2. Output

1) 세 점으로 부메랑을 만들 수 있다면 true, 없다면 false를 반환

- 부메랑은 한 직선 위에 있지 않은 서로 다른 세 점을 말한다.

 

3. Constraint

1) points.length == 3

2) points[i].length == 2

3) 0 <= xi, yi <= 100

 

4. Example

Input: points = [[1,1],[2,3],[3,2]] -> Output: true

Input: points = [[1,1],[2,2],[3,3]] -> Output: false

 

5. Code

1) 첫 코드(2023/04/04)

boolean answer;
int x1 = points[1][0] - points[0][0];
int x2 = points[2][0] - points[1][0];
double y1 = points[1][1] - points[0][1];
double y2 = points[2][1] - points[1][1];
if((x1==0 && y1==0) || (x2==0 && y2==0))
    answer = false;
else if(x1!=0 && x2!=0)
    answer = y1/x1 != y2/x2;
else{
    if(x1==0)
        answer = x2!=0;
    else
        answer = x1!=0;
}
return answer;