[LeetCode] Power of Three

326. Power of Three

Given an integer n, return true if it is a power of three. Otherwise, return false.

An integer n is a power of three, if there exists an integer x such that n == 3x.

Follow up: Could you solve it without loops/recursion?

1
2
3
4
5
6
class Solution {
public:
bool isPowerOfThree(int n) {
return n > 0 and !((int)pow(3,19) % n);
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/02/09/PS/LeetCode/power-of-three/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.