File tree Expand file tree Collapse file tree 2 files changed +38
-0
lines changed Expand file tree Collapse file tree 2 files changed +38
-0
lines changed Original file line number Diff line number Diff line change
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
+
Original file line number Diff line number Diff line change
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
You can’t perform that action at this time.
0 commit comments