Leetcode # 231. Power of Two

Problem

https://leetcode.com/problems/power-of-two

Solution

Time Complexity: O(log(n))
Space Complexity: O(1)
(The input and output generally do not count towards the space complexity.)

class Solution:
  def isPowerOfTwo(self, n: int) -> bool:
    while n > 1:
      if n % 2 != 0:
        return False
      n //= 2
    return n > 0

 

Last Updated on 2025/08/09 by A1go

目錄
Bitnami