[LeetCode] Power of Three

아르당·2026년 1월 2일

LeetCode

목록 보기
70/94
post-thumbnail

문제를 이해하고 있다면 바로 풀이를 보면 됨
전체 코드로 바로 넘어가도 됨
마음대로 번역해서 오역이 있을 수 있음

Problem

정수 n이 주어졌을 때, 3의 거듭제곱이면 true를 반환해라. 그렇지 않으면 false를 반환해라.
정수 n이 3의 거듭제곱이라는 것은 n == 3^x를 만족하는 정수 x가 존재한다는 것이다.

Example

#1
Input: n = 27
Output: true
Explanation: 27 = 3^3

#2
Input: n = 0
Output: false
Explanation: 3^x = 0이 되는 x는 없다.

#3
Input: n = -1
Output: false
Explanation: 3^x = -1이 되는 x는 없다.

Constraints

  • -2^31 <= n <= 2^31 - 1

Solved

class Solution {
    public boolean isPowerOfThree(int n) {
        int maxPowerOfThree = 1162261467;

        return n > 0 && maxPowerOfThree % n == 0;
    }
}
profile
내 마음대로 코드 작성하는 세상

0개의 댓글