코딩 테스트 풀이 🛠
[Leet Code - eazy] 2413. Smallest Even Multiple
엄성준
2023. 1. 29. 02:05
728x90
Given a positive integer n, return the smallest positive integer that is a multiple of both 2 and n.
Example 1:
Input: n = 5
Output: 10
Explanation: The smallest multiple of both 5 and 2 is 10.
Example 2:
Input: n = 6
Output: 6
Explanation: The smallest multiple of both 6 and 2 is 6. Note that a number is a multiple of itself.
Constraints:
- 1 <= n <= 150
나의 풀이
1
2
3
4
5
6
7
8
9
10
11
|
/**
* @param {number} n
* @return {number}
*/
var smallestEvenMultiple = function(n) {
for(let i=1; i<=(n*2); i++){
if(i % n === 0 && i % 2===0){
return i
}
}
};
|
cs |
문제를 풀며 느낀 점
- 2와 n의 최소 공배수를 구하는 문제라서 for문을 1부터 n*2까지 반복하는 중에 만약에 i을 각각 n과 2로 나눴을 때 0이라면 첫 번째로 걸린 i를 return 해주었습니다.
728x90