题目
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 {
public boolean isPowerOfThree(int n) {
if(n==0){
return false;
}
if(n==1){
return true;
}
else if(n%3==0)
{
return isPowerOfThree(n/3);
}
else{
return false;
}
}
}
解2(非递归,失败(精度不够))
public class Solution {
public boolean isPowerOfThree(int n) {
Double logAnss=new Double(Math.log(n) / Math.log(3));
return (logAnss- logAnss.intValue() == 0) ? true : false;
}
}
解3(非递归,成功)
public class Solution {
public boolean isPowerOfThree(int n) {
Double logAnss=new Double(Math.log10(n) / Math.log10(3));
return (logAnss- logAnss.intValue() == 0) ? true : false;
}
}
参考链接
参考链接1:http://soft.chinabyte.com/database/408/11329908.shtml
参考链接2:http://bbs.youkuaiyun.com/topics/10345440
参考链接3:
http://blog.163.com/xiaohui_1123@126/blog/static/3980524020104141224835/
参考链接4:http://m.blog.youkuaiyun.com/article/details?id=50540517