Post

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

정수 num1과 num2가 같으면 1, 다르면 -1을 반환하는 워밍업 문제.

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

문제 링크


1. 아이디어

정수 num1, num2가 주어질 때, 두 값이 같으면 1을, 다르면 -1을 반환하면 되는 간단한 문제다.


2. 복잡도

접근시간공간
풀이$O(1)$$O(1)$

3. 코드

풀이 [Java][C++][Python]

1
2
3
4
5
class Solution {
    public int solution(int num1, int num2) {
        return num1 == num2 ? 1 : -1;
    }
}
1
2
3
4
5
6
#include <bits/stdc++.h>
using namespace std;

int solution(int num1, int num2) {
    return num1 == num2 ? 1 : -1;
}
1
2
def solution(num1, num2):
    return 1 if num1 == num2 else -1

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