LeetCode – Power of Two (Java)
Given an integer, write a function to determine if it is a power of two.
https://leijiangcoding.wordpress.com/2015/07/
http://bookshadow.com/weblog/2015/07/06/leetcode-power-of-two/
def isPowerOfTwo(self, n): return n > 0 and n & (n - 1) == 0
Read full article from LeetCode – Power of Two (Java)
Given an integer, write a function to determine if it is a power of two.
public boolean isPowerOfTwo(int n) { if(n<=0) return false; while(n>2){ int t = n>>1; int c = t<<1; if(n-c != 0) return false; n = n>>1; } return true; } |
bool
isPowerOfTwo(
int
n) {
long
testNum = 1;
while
(n >= testNum){
if
(n == testNum)
return
true
;
testNum <<= 1;
}
return
false
;
}
def isPowerOfTwo(self, n): return n > 0 and n & (n - 1) == 0
Read full article from LeetCode – Power of Two (Java)