Given two numbers represented as strings, return multiplication of the numbers as a string.
Note:
- The numbers can be arbitrarily large and are non-negative.
- Converting the input string to integer is NOT allowed.
- You should NOT use internal library such as BigInteger.
class Solution(object):
def multiply(self, num1, num2):
la = len(num1)
lb = len(num2)
a = []
b = []
for i in num1:
a += int(i),
for i in num2:
b += int(i),
a = a[::-1]
b = b[::-1]
res = [0] * (la+lb)
l = min(la,lb)
for i in xrange(la):
for j in xrange(lb):
t = a[i] * b[j]
res[i+j] += t
for i in xrange(len(res)-1):
res[i + 1] += res[i] / 10
res[i] %= 10
res = res[::-1]
pos = 0
while pos < len(res) and res[pos] == 0:
pos += 1
res = res[pos:]
ress = ''
for i in res:
ress += str(i)
if ress == '':
ress = '0'
return ress