Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions 0067.Add-Binary/memo.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# 67. Add Binary

## step1
組み込み関数を使えば簡単。

## step2

`itertools.zip_longest` を使う。二進法の加算を愚直に実装。

https://github.com/NobukiFukui/Grind75-ProgrammingTraining/pull/35


https://github.com/huyfififi/coding-challenges/pull/20

> deque() を使うと reverse 処理が省けて、かつ、手計算のときと同じように考えられるのでいいですね


https://github.com/ryosuketc/leetcode_grind75/pull/20


zfillは知らなかった。
https://docs.python.org/ja/3/library/stdtypes.html#str.zfill
3 changes: 3 additions & 0 deletions 0067.Add-Binary/step1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Solution:
def addBinary(self, a: str, b: str) -> str:
return bin(int(a, 2) + int(b, 2))[2:]
67 changes: 67 additions & 0 deletions 0067.Add-Binary/step2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import itertools


class Solution:
def addBinary(self, a: str, b: str) -> str:
reversed_result = []
carry = False
for c_a, c_b in itertools.zip_longest(a[::-1], b[::-1], fillvalue="0"):
if c_a == "0" and c_b == "0" and carry:
reversed_result.append("1")
carry = False
elif c_a == "0" and c_b == "0" and not carry:
reversed_result.append("0")
carry = False
elif c_a == "1" and c_b == "1" and carry:
reversed_result.append("1")
carry = True
elif c_a == "1" and c_b == "1" and not carry:
reversed_result.append("0")
carry = True
elif carry:
reversed_result.append("0")
carry = True
else:
reversed_result.append("1")
carry = False
if carry:
reversed_result.append("1")
return "".join(reversed(reversed_result))


class Solution:
def addBinary(self, a: str, b: str) -> str:
i = len(a) - 1
j = len(b) - 1
carry = 0
reversed_result = []
while i >= 0 or j >= 0 or carry == 1:
total = carry
if i >= 0:
total += int(a[i])
if j >= 0:
total += int(b[j])
reversed_result.append(str(total % 2))
carry = total // 2
i -= 1
j -= 1
return "".join(reversed(reversed_result))


from collections import deque


class Solution:
def addBinary(self, a: str, b: str) -> str:
n = max(len(a), len(b))
padded_a = a.zfill(n)
padded_b = b.zfill(n)
result = deque()
carry = 0
for i in range(n - 1, -1, -1):
total = int(padded_a[i]) + int(padded_b[i]) + carry
result.appendleft(str(total % 2))
carry = total // 2
if carry == 1:
result.appendleft("1")
return "".join(result)