Scribbling

LeetCode: 67. Add Binary 본문

Computer Science/Coding Test

LeetCode: 67. Add Binary

focalpoint 2021. 9. 26. 15:23
class Solution:
    def addBinary(self, a: str, b: str) -> str:
        if len(b) > len(a):
            a, b = b, a
        b = '0'*(len(a)-len(b)) + b
        ret = ''
        carry = 0
        for i in range(len(a)-1, -1, -1):
            temp = int(a[i]) + int(b[i]) + carry
            ret += str(temp%2)
            carry = temp//2
        if carry:
            ret += str(1)
        return ret[::-1]

 

'Computer Science > Coding Test' 카테고리의 다른 글

LeetCode: 71. Simplify Path  (0) 2021.09.26
LeetCode: 69. Sqrt(x)  (0) 2021.09.26
LeetCode: 64. Minimum Path Sum  (0) 2021.09.24
LeetCode: 63. Unique Paths II  (0) 2021.09.24
LeetCode: 62. Unique Paths  (0) 2021.09.24