Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 81d7822

Browse files
committed
Added 67 & 69
1 parent 11d425b commit 81d7822

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

Python3/67-Add_Binary.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution:
2+
def addBinary(self, a: str, b: str) -> str:
3+
max_len = max(len(a), len(b))
4+
5+
a = a.zfill(max_len)
6+
b = b.zfill(max_len)
7+
8+
# Initialize the result
9+
result = ''
10+
11+
# Initialize the carry
12+
carry = 0
13+
14+
# Traverse the string
15+
for i in range(max_len -1, -1, -1):
16+
r = carry
17+
r += 1 if a[i] == '1' else 0
18+
r += 1 if b[i] == '1' else 0
19+
result = ('1' if r % 2 == 1 else '0') + result
20+
carry = 0 if r < 2 else 1 # Complete the carry
21+
22+
if carry !=0 : result = '1' + result
23+
24+
return result.zfill(max_len)
25+

Python3/69-Sqrt.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution:
2+
def mySqrt(self, x: int) -> int:
3+
# Start by assuming b is equal to x
4+
b = x
5+
6+
# Use the Newton-Raphson formula to improve the estimate iteratively
7+
while b * b > x:
8+
9+
# Continue the iteration until (b \times b) is less than or equal to x, meaning that b is the largest integer whose square is less than or equal to x
10+
b = (b + x // b) // 2
11+
12+
# After the loop, return b as the integer part of the square root
13+
return b

0 commit comments

Comments
 (0)