-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleet_0043_multiply_strings.py
More file actions
34 lines (32 loc) · 1006 Bytes
/
leet_0043_multiply_strings.py
File metadata and controls
34 lines (32 loc) · 1006 Bytes
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
class Solution:
def multiply(self, num1: str, num2: str) -> str:
if num1 == '0' or num2 == '0':
return '0'
len1 = len(num1)
len2 = len(num2)
zero = ord('0')
p = [0] * (len1 + len2)
res = ''
for i in range(len1 - 1, -1, -1):
for j in range(len2 - 1, -1, -1):
# way 1
L = ord(num1[i]) - zero
R = ord(num2[j]) - zero
temp = L * R + p[i + j + 1]
p[i + j] = temp // 10 + p[i + j]
p[i + j + 1] = temp % 10
# way 2
"""
L = i + j
R = i + j + 1
tmp = (ord(num1[i]) - zero) * (ord(num2[j]) - zero) + p[R]
p[L] = tmp // 10 + p[L]
p[R] = tmp % 10
"""
i = 0
while p[i] == 0:
i += 1
while i < len1 + len2:
res += str(p[i])
i += 1
return res