diff --git a/leetcode/src/231.c b/leetcode/src/231.c index a2d3b1e7..81ea2b04 100644 --- a/leetcode/src/231.c +++ b/leetcode/src/231.c @@ -1,7 +1,6 @@ -bool isPowerOfTwo(int n) -{ - if (!n) - return false; - while (n % 2 == 0) n /= 2; - return n == 1; -} \ No newline at end of file +// Without loops/recursion. +// Runtime: O(1) +// Space: O(1) +bool isPowerOfTwo(int n){ + return (n > 0) && ((n & (n - 1)) == 0); +}