Skip to content

Commit 1224b6b

Browse files
add a better 43's python solution
1 parent ff51591 commit 1224b6b

File tree

1 file changed

+7
-18
lines changed

1 file changed

+7
-18
lines changed

src/0043-Multiply-Strings/0043.py

+7-18
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,8 @@
11
class Solution:
2-
def multiply(self, num1, num2):
3-
"""
4-
:type num1: str
5-
:type num2: str
6-
:rtype: str
7-
"""
8-
num1_len, num2_len = len(num1), len(num2)
9-
res = ['0'] * (num1_len + num2_len)
10-
for i in range(num2_len-1, -1, -1):
11-
for j in range(num1_len-1, -1, -1):
12-
tmp = int(num1[j]) * int(num2[i]) + int(res[i + j + 1])
13-
res[i + j + 1] = str(tmp%10)
14-
res[i + j] = str(int(res[i+j]) + tmp // 10)
15-
16-
for i in range(num1_len + num2_len):
17-
if res[i] != '0':
18-
return ''.join(res[i:])
19-
return '0'
2+
def multiply(self, num1: str, num2: str) -> str:
3+
res, m, n = 0, len(num1), len(num2)
4+
5+
for i in range(1, m + 1):
6+
for j in range(1, n + 1):
7+
res += int(num1[-i]) * int(num2[-j]) * 10 ** (i + j - 2)
8+
return str(res)

0 commit comments

Comments
 (0)