Given an integer, write a function to determine if it is a power of two.
javascript
思路:一個數若是不是2^n,那麼對這個數反覆作除二操做最後的餘數必定是1.
java
language-javascriptspa
/** * @param {number} n * @return {boolean} */ var isPowerOfTwo = function(n) { if(n<=0){ return false; } while(n){ if((n%2==1) && n!=1){ return false; } n/=2; } return true; };