原题链接在这里:https://leetcode.com/problems/power-of-two/
思路:n一直除以2,直到1,若中间过程出现非偶数,就返回false。
Note: 1 也是power of two, 2的零次方。
AC Java:
public class Solution {
public boolean isPowerOfTwo(int n) {
if(n<1)
return false;
while(n!=1.0){
if((n%2) !=0)
return false;
n/=2;
}
return true;
}
}