326-power-of-three
Question
https://leetcode.com/problems/power-of-three/description/
Given an integer, write a function to determine if it is a power of three.
Example:
Thought Process
- While Loop
- Time complexity O(log n)
- Space complexity O(1)
- Recursion
- Time complexity O(log n)
- Space complexity O(log n)
- Base Conversion
- Leveraging the built-in function in converting number to base form, the base 2 for number that is power of 2 will look like 10*
- Likewise for 3, if we convert the number on the basis of 3, we can match the resulted string with regex
- Time complexity O(log n)
- Space complexity O(log n)
- Math
- Since all the integer is range from -2^31 to 2^31 - 1, we can find the maximum power of 3 by find the power first and round down and take the p
- int pow = (int) (Math.log(Integer.MAX_VALUE) / Math.log(3))
- int max3power = Math.pow(3, pow) = 1162261467
- And 3, 9, 27..., all the power of 3 are the only divisors for this number
- Time complexity O(1)
- Space complexity O(1)
Solution
class Solution {
public boolean isPowerOfThree(int n) {
if (n < 1) return false;
while (n % 3 == 0) n /= 3;
return n == 1;
}
}
class Solution {
public boolean isPowerOfThree(int n) {
if (n <= 0) return false;
if (n == 1) return true;
return (n % 3 == 0) && isPowerOfThree(n/3);
}
}
class Solution {
public boolean isPowerOfThree(int n) {
return Integer.toString(n, 3).matches("^10*$");
}
}
class Solution {
public boolean isPowerOfThree(int n) {
int pow = (int) (Math.log(Integer.MAX_VALUE) / Math.log(3));
return n > 0 && (int) Math.pow(3, pow) % n == 0;
// return n > 0 && 1162261467 % 3 == 0;
}
}