Hyoseo Lee
Home About Projects Blog Thinking
leetcode

326. Power of Three

Topic Math
Area Algorithms
Summary
compare the number with all the possible power of threes.

Problem

View on LeetCode →

Difficulty: Easy
Tags: Math, Recursion

Intuition

I believed that there won’t be that much power of threes in the range of integer, so I just listed them. i copied from the discussion board, but that was exactly what I was trying to do.

Approach

compare the number with all the possible power of threes.

Solution

bool isPowerOfThree(int n) {
    return n == 1 || n == 3 || n == 9 || n == 27 || n == 81 || n == 243 || n == 729 || n == 2187 || n == 6561 || n == 19683 || n == 59049 || n == 177147 || n == 531441 || n == 1594323 || n == 4782969 || n == 14348907 || n == 43046721 || n == 129140163 || n == 387420489 || n == 1162261467 || n == 3486784401 || n == 10460353203 || n == 31381059609 || n == 94143178827 || n == 282429536481 || n == 847288609443 || n == 2541865828329 || n == 7625597484987 || n == 22876792454961 || n == 68630377364883 || n == 205891132094649 || n == 617673396283947 || n == 1853020188851841 || n == 5559060566555523 || n == 16677181699666568 || n == 50031545098999710 || n == 150094635296999140 || n == 450283905890997400 || n == 1350851717672992000 || n == 4052555153018976000 ;
}

thougths

this was interesting question, but there wasn’t anything to learn.

Complexity