Development/Algorithms
Leetcode 67. Add Binary
juniz
2020. 6. 7. 21:30
반응형
Description
Given two binary strings, return their sum (also a binary string).
The input strings are both non-empty and contains only characters 1 or 0.
Answer
class Solution:
def addBinary(self, a: str, b: str) -> str:
i = int(a,2)
j = int(b,2)
# ans = i+j
# return bin(ans)[2:]
return format(i+j,'b')
진수 변환
>> bin(30)
'0b1110'
>> oct(30)
'0o36'
>> hex(30)
'0x1e'
Format 함수
# 접두어 제외하기
>> format(0b11110, 'b')
'11110'
# 접두어 포함
>> format(30, '#b')
'11110'
반응형