power-of-two Posted on 2017-12-17 Edited on 2021-06-17 Power of Two – LeetCode 231ProblemDescriptionGiven an integer, write a function to determine if it is a power of two. AnswerOriginalCode123456class Solution {public: bool isPowerOfTwo(int n) { return (n > 0) && (!(n & (n - 1))); }}; 思路利用位操作,时间复杂度$O(1)$,空间复杂度$O(1)$。耗时$3$ ms,排名$78.24\%$ Better思路目前还没看到更好的解法。