标签:
原题链接在这里:https://leetcode.com/problems/power-of-three/
与Power of Two类似。检查能否被3整除,然后整除,再重复检查结果。
Time Complexity: O(logn). n是原始数字。Space: O(1).
AC Java:
1 public class Solution { 2 public boolean isPowerOfThree(int n) { 3 if(n < 1){ 4 return false; 5 } 6 while(n > 1){ 7 if(n%3 == 1 || n%3 == 2){ 8 return false; 9 } 10 n/=3; 11 } 12 return true; 13 } 14 }
标签:
原文地址:http://www.cnblogs.com/Dylan-Java-NYC/p/5115116.html