LeetCode 1780. Check if Number is a Sum of Powers of Three
Input: n = 12
Output: true
Explanation: 12 = 31 + 32Input: n = 91
Output: true
Explanation: 91 = 30 + 32 + 34Input: n = 21
Output: falseSolution:
class Solution {
public:
bool checkPowersOfThree(int n) {
int tmp = 3;
while (tmp < n) {
tmp *= 3;
}
while (tmp > 0) {
if (n >= tmp) {
n -= tmp;
}
tmp = tmp/3;
}
return n == 0;
}
};PreviousLeetCode 1779. Find Nearest Point That Has the Same X or Y CoordinateNextLeetCode 1781. Sum of Beauty of All Substrings
Last updated