Medium
Check if Number is a Sum of Powers of Three — C++
Full explanation · Time O(logn) · Space O(1)
// Time: O(logn)
// Space: O(1)
class Solution {
public:
bool checkPowersOfThree(int n) {
for (; n > 0 && n % 3 != 2; n /= 3);
return n == 0;
}
};