231. Power of Two
来源:互联网 发布:淘宝自动下架的原因 编辑:程序博客网 时间:2024/05/22 04:26
Given an integer, write a function to determine if it is a power of two.
Credits:
Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.
class Solution {public: bool isPowerOfTwo(int n) { if(n<=0) return 0;int count=0;while(n){if(n%2==1) count++;if(count>1) return false;n/=2;}return true; }};
阅读全文