本文共 642 字,大约阅读时间需要 2 分钟。
4,临界条件:(n>0, 如果n<=0, 这题就没卵用)
class Solution {public: bool isPowerOfTwo(int n) { int cnt = 0; while (n > 0){ cnt += (n & 1); n >>= 1; } return cnt == 1; }};
class Solution {public: bool isPowerOfTwo(int n) { return (n > 0) && !(n & (n-1)); }};
转载地址:http://qcpbb.baihongyu.com/