Leetcode 43. Multiply Strings
题目
解法:
class Solution:
def multiply(self, num1: str, num2: str) -> str:
if num1 == '0' or num2 == '0':
return '0'
# initialize the product as a list, the length of the product of these two num won't exceed the sum of length of num1 and num2
product = [0] * (len(num1) + len(num2))
# initialize the operation position, starting from the end of the product list
pos = len(product) - 1
for n1 in reversed(num1):
# store the position for multiplying a single char in num1 with the entire num2
curr_pos = pos
for n2 in reversed(num2):
# add the results at corresponding position
product[curr_pos] += (ord(n1) - ord('0')) * (ord(n2) - ord('0'))
# take care of the carry
product[curr_pos - 1] += product[curr_pos] // 10
product[curr_pos] %= 10
curr_pos -= 1
pos -= 1
ans = ''
# removed the starting 0s
i = 0
while i < len