1 public class Solution { 2 public boolean isPowerOfThree(int n) { 3 while (n > 1) { 4 if (n % 3 != 0) { 5 return false; 6 } 7 n /= 3; 8 } 9 return n == 1; 10 } 11 }
1 public class Solution { 2 public boolean isPowerOfThree(int n) { 3 double a = Math.log(n) / Math.log(3); 4 return Math.abs(a - Math.rint(a)) <= 0.00000000000001; 5 } 6 }
时间: 2024-10-05 04:48:41