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 해주었습니다.
'코딩 테스트 풀이 🛠' 카테고리의 다른 글
[Leet Code - eazy] 2160. Minimum Sum of Four Digit Number After Splitting Digits (0) | 2023.01.30 |
---|---|
[Leet Code - eazy] 2114. Maximum Number of Words Found in Sentences (0) | 2023.01.29 |
[Leet Code - eazy] 1672. Richest Customer Wealth (0) | 2023.01.28 |
[Leet Code - eazy] 771. Jewels and Stones (0) | 2023.01.28 |
[Leet Code - eazy] 1512. Number of Good Pairs (1) | 2023.01.28 |