[Programmers] #181937 - n의 배수 [Java][C++][Python]
정수 num이 n의 배수인지 판별하는 워밍업 문제.
[Programmers] #181937 - n의 배수 [Java][C++][Python]
1. 아이디어
정수 num이 n의 배수인지 판별하는 워밍업 문제다. num을 n으로 나눈 나머지가 0이면 배수이므로 1을, 그렇지 않으면 0을 반환하면 된다.
2. 복잡도
| 접근 | 시간 | 공간 |
|---|---|---|
| 풀이 | $O(1)$ | $O(1)$ |
3. 코드
풀이 [Java][C++][Python]
1
2
3
4
5
class Solution {
public int solution(int num, int n) {
return num % n == 0 ? 1 : 0;
}
}
1
2
3
4
5
6
#include <bits/stdc++.h>
using namespace std;
int solution(int num, int n) {
return num % n == 0;
}
1
2
def solution(num, n):
return int(num % n == 0)
This post is licensed under CC BY 4.0 by the author.