231. Power of Two
My SubmissionsTotal Accepted: 43958 Total Submissions: 131591 Difficulty: EasyGiven 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.
Subscribe to see which companies asked this question
Hide Tags Math Bit Manipulation Hide Similar Problems (E) Number of 1 Bits//思路首先:如果一個數是2的某個次方,那麼他一定不會小於等於0(最多無限接近0) //如果n是2的某個次方,那麼他的二進制形式一定是10000......這樣的形式 //並且n-1的二進制形式一定是01111....,進行與運算一定是false class Solution { public: bool isPowerOfTwo(int n) { if(n <= 0) return false; if(n&n-1) return false; else return true; } };