Last updated 5 years ago
Was this helpful?
Given an integer, write a function to determine if it is a power of three.
Follow up: Could you do it without using any loop / recursion?
class Solution { public boolean isPowerOfThree(int n) { while (n > 0 && n % 3 == 0) { n = n/3; } return n == 1; } }
时间复杂度O(logn), 空间复杂度O(1)