Power of Three

发布时间:2016-12-31 7:29:10
来源:分享查询网

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?

 1 public class Solution { 2     public boolean isPowerOfThree(int n) { 3         if (n == 0)    return false; 4  5         if (n == 1) return true; 6  7         if (n > 1) 8             return n % 3 == 0 && isPowerOfThree(n / 3); 9         else10             return false;11     }12 }

返回顶部
查看电脑版