Post

[Programmers] 120807번 - 숫자 비교하기 [Java][C++]

[Programmers] 120807번 - 숫자 비교하기 [Java][C++]

문제 링크


1. 아이디어

정수 num1num2가 같으면 1 다르면 -1을 return하는 문제로 조건 분기를 통해 해결하면 된다.


2. 복잡도

시간복잡도공간복잡도
$O(1)$$O(1)$

3. 코드

풀이 [Java][C++]

삼항 연산자를 활용해서 같은 경우와 다른 경우를 분기해줬다.

1
2
3
4
5
class Solution {
    public int solution(int num1, int num2) {
        return num1 == num2 ? 1 : -1;
    }
}
1
2
3
int solution(int num1, int num2) {
    return num1 == num2 ? 1 : -1;
}

This post is licensed under CC BY 4.0 by the author.