https://youtu.be/keuWJ47xG8g?si=9FQTur6Jph3SV28p
class Solution:
def addBinary(self, a: str, b: str) -> str:
res = ""
carry = 0
a, b = a[::-1], b[::-1]
for i in range(max(len(a), len(b))):
digitA = int(a[i]) if i < len(a) else 0
digitB = int(b[i]) if i < len(b) else 0
sumAB = digitA + digitB + carry
carry = sumAB // 2
res += str(sumAB % 2)
if carry: # carry == 1
res += str(carry)
return res[::-1]
'LeetCode > Top Interview 150' 카테고리의 다른 글
201. Bitwise AND of Numbers Range (0) | 2024.12.18 |
---|---|
137. Single Number II (0) | 2024.12.18 |
Graphs (싸이클탐지): 210. Course Schedule II★★★ (2) | 2024.12.17 |
530. Minimum Absolute Difference in BST (0) | 2024.12.15 |
103. Binary Tree Zigzag Level Order Traversal (0) | 2024.12.15 |