ice rabbit programming

[LeetCode] 231. Power of Two 본문

PS/LeetCode

[LeetCode] 231. Power of Two

판교토끼 2020. 12. 8. 23:37

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

 

Power of Two - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

제목 그대로, 입력된 숫자가 2의 제곱수인지를 판별하는 문제이다. 고전적으로 계속 2로 나누는 방식을 선택했는데, 무리없이 해결되었다.

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

'PS > LeetCode' 카테고리의 다른 글

[LeetCode] 766. toeplitz-matrix  (0) 2022.11.10
[LeetCode] 242. Valid Anagram  (0) 2022.10.17
[LeetCode] 217. Contains Duplocate  (0) 2020.12.08
[LeetCode] 191. Numbers of 1 Bits  (0) 2020.12.08
[LeetCode] 190. Reverse Bits  (0) 2020.12.08