-
Notifications
You must be signed in to change notification settings - Fork 2
/
sol.py
54 lines (47 loc) · 1.59 KB
/
sol.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
from typing import List
class Solution:
def sum(self, num1: str, num2: str) -> str:
longer = max(len(num1), len(num2))
remainder = 0
total = []
for i in range(longer):
v1 = num1[-(i+1)] if i < len(num1) else 0
v2 = num2[-(i+1)] if i < len(num2) else 0
subTotal = int(v1) + int(v2) + remainder
if subTotal > 9:
remainder = 1
else:
remainder = 0
left = subTotal % 10
total.insert(0, str(left))
if remainder:
total.insert(0, str(remainder))
return "".join(total)
def multWithSingle(self, numStr: str, digit: int) -> str:
if digit == 0:
return "0"
remainder = 0
total = []
for i in range(len(numStr)):
v = int(numStr[-(i+1)])
curMult = v * digit + remainder
remainder = curMult // 10
curLeftDigit = curMult % 10
total.insert(0, str(curLeftDigit))
if remainder:
total.insert(0, str(remainder))
return "".join(total)
def multiply(self, num1: str, num2: str) -> str:
total = "0"
for i in range(len(num1)):
digit = int(num1[-(i+1)])
multWithOther = self.multWithSingle(num2, digit)
if multWithOther != "0":
multWithOther = multWithOther + "0"*i
total = self.sum(total, multWithOther)
return str(total)
s = Solution()
res = s.multiply('9111', '0')
# res = s.sum('99', '1')
# res = s.multWithSingle('9111', 0)
print(res)