feat: improve the Power of Two LeetCode problem (#1148)

This commit is contained in:
Alexander Pantyukhin 2022-11-19 00:09:44 +04:00 committed by GitHub
parent ea775e7a04
commit 0bcabd6897
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 6 additions and 7 deletions

View File

@ -1,7 +1,6 @@
bool isPowerOfTwo(int n)
{
if (!n)
return false;
while (n % 2 == 0) n /= 2;
return n == 1;
}
// Without loops/recursion.
// Runtime: O(1)
// Space: O(1)
bool isPowerOfTwo(int n){
return (n > 0) && ((n & (n - 1)) == 0);
}