-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path043. Multiply Strings.py
42 lines (31 loc) · 1.25 KB
/
043. Multiply Strings.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
# Given two non-negative integers num1 and num2 represented as strings, return the product of num1 and num2.
# Note:
# The length of both num1 and num2 is < 110.
# Both num1 and num2 contains only digits 0-9.
# Both num1 and num2 does not contain any leading zero.
# You must not use any built-in BigInteger library or convert the inputs to integer directly.
# must see the Leetcode First Discussion
class Solution(object):
def multiply(self, num1, num2):
"""
:type num1: str
:type num2: str
:rtype: str
"""
m = len(num1)
n = len(num2)
pos = [0 for _ in range(m+n)]
for i in range(m-1,-1,-1):
for j in range(n-1,-1,-1):
second_position = i+j
first_position = i+j+1
original = int(num1[i]) * int(num2[j])
temp = original + pos[first_position]
pos[second_position] += temp / 10 ## 15/10 = 1
pos[first_position] = temp % 10 ## 15%10 = 5
res = ""
for i in pos:
if not res and i == 0:
continue
res += str(i)
return res if res else '0'