[Python] isPowerOfTwo

숭글·2023년 4월 11일
0

LeetCode 231번 문제를 해결했다!

class Solution:
    def isPowerOfTwo(self, n: int) -> bool:
        if n < 0 : return False

        num = str(bin(n)[2:])

        return num.count("1") == 1

파이썬의 bin(), str()함수를 이용해 해결했는데 제출 후 서브밋을 보니 생각지못한 방법을 발견했다.

return n&(n-1)==0   

return 2**int(math.log2(n)) == n

ㅇ0ㅇ~! 까먹지 말고 나중에 꼭 써먹어야지!


LeetCode 231

profile
Hi!😁 I'm Soongle. Welcome to my Velog!!!

0개의 댓글