/*****************************************************问题描述*************************************************
Given an integer, write a function to determine if it is a power of two.
Example 1:
Input: 1
Output: true
Explanation: 20 = 1
Example 2:
Input: 16
Output: true
Explanation: 24 = 16
Example 3:
Input: 218
Output: false
给定一个整数,判断它是否是2的n次方
/*****************************************************我的解答*************************************************
/**
* @param {number} n
* @return {boolean}
*/
var isPowerOfTwo = function(n) {
if(n == 1)
{
return true;
}
if(n <= 0)
{
return false;
}
var tempArray = n.toString(2).split('').sort();
if((tempArray[tempArray.length - 1] == '0') || (tempArray[tempArray.length - 1] == '1' && tempArray[tempArray.length - 2] == '0'))
{
return true;
}
else
{
return false;
}
};
console.log(isPowerOfTwo(17));
leetCode刷题记录35_231_Power of Two
猜你喜欢
转载自blog.csdn.net/gunsmoke/article/details/87939023
今日推荐
周排行