반응형
https://leetcode.com/problems/power-of-three/
Power of Three - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
수의 범위가 int 형이기 때문에,
2^31 - 1 이하에서 가장 큰 3의 제곱 수와 n의 나머지가 0이라면, 3의 제곱 수입니다.
class Solution {
public:
bool isPowerOfThree(int n) {
return n > 0 && !(1162261467 % n);
}
};
반응형
'Algorithm' 카테고리의 다른 글
LeetCode 987 : Vertical Order Traversal of a Binary Tree (0) | 2022.09.04 |
---|---|
LeetCode 967 : Numbers With Same Consecutive Differences (0) | 2022.09.03 |
LeetCode 1338 : Reduce Array Size to The Half (0) | 2022.08.18 |
LeetCode 804 : Unique Morse Code Words (0) | 2022.08.17 |
LeetCode 387 : First Unique Character in a String (0) | 2022.08.16 |