본문 바로가기
Leetcode

[LeetCode] Add Binary 파이썬 (int, bin)

by YGSEO 2021. 3. 30.
728x90

파이썬 내장 int, bin을 사용하면 10 to 2, 2 to 10이 가능

단 bin으로 했을 경우 0b100 이런식으로 str으로 return이 되기 때문에 slicing 필요

36ms

class Solution:
    def addBinary(self, a: str, b: str) -> str:
        
        ans = int(a,base=2)+int(b,base=2)
        ans = bin(ans)[2:]
        return ans

한줄로는 그냥 이렇게 32ms

4ms 단축

return bin(int(a,2)+int(b,2))[2:]
728x90

댓글