Last updated 5 years ago
https://leetcode.com/problems/power-of-two/
public class Solution { public boolean isPowerOfTwo(int n) { if (n <= 0) { return false; } return ((n - 1) & n) == 0; } }
这道题里认为 0 不算平方,所以开头要把它排除
做 & 运算加括号!否则会先判断 n == 0
return ((n - 1) & n) == 0;