题目
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.
python
class Solution(object):
def function(self,num1, num2):
if num2 == 0:
return '0'
increase = 0
result = ''
length = len(num1) - 1
while length >= 0:
tem = int(num1[length]) * num2 + increase
increase = tem // 10
result = str(tem % 10) + result
length -= 1
if increase > 0:
result = str(increase) + result
return result
def add(self,num1, num2):
result = ''
length1 = len(num1) - 1
length2 = len(num2) - 1
increase = 0
while length1 >= 0 and length2 >= 0:
tem = int(num2[length2]) + int(num1[length1]) + increase
increase = tem // 10
result = str(tem % 10) + result;
length1 -= 1
length2 -= 1
if length1 < 0:
length1 = length2
num1 = num2
while length1 >= 0:
tem = int(num1[length1]) + increase
increase = tem // 10
result = str(tem % 10) + result;
length1 -= 1
if increase > 0:
result = str(increase) + result;
return result
def multiply(self, num1, num2):
"""
:type num1: str
:type num2: str
:rtype: str
"""
if num1 == '0' or num2 == '0':
return '0'
result = '0'
length = len(num2)
for i in range(length):
tem = self.function(num1, int(num2[length - 1 - i]))
result = self.add(result, tem + '0' * i)
return result