mirror of
https://github.moeyy.xyz/https://github.com/TheAlgorithms/C.git
synced 2023-10-11 15:56:24 +08:00
feat: improve the Power of Two LeetCode problem (#1148)
This commit is contained in:
parent
ea775e7a04
commit
0bcabd6897
@ -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);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user