/*****************************************************问题描述*************************************************
Given an integer (signed 32 bits), write a function to check whether it is a power of 4.
Example 1:
Input: 16
Output: true
Example 2:
Input: 5
Output: false
Follow up: Could you solve it without loops/recursion?
判断一个数是否是4的幂
/*****************************************************我的解答*************************************************
//以下是用循环做的,其实速度也可以,如果不这么搞可以像之前判断2的幂那样用位运算去搞
/**
* @param {number} n
* @return {boolean}
*/
var isPowerOfThree = function(n) {
if(n <= 0)
{
return false;
}
while(n != 1)
{
if(n % 3 != 0)
{
return false;
}
else
{
n = parseInt(n / 3);
}
}
return true;
};
leetCode刷题记录45_342_Power of Four
最新推荐文章于 2020-03-06 21:03:30 发布