2020-08-13
43. 字符串相乘
题解: 模拟两个字符串相乘的过程,字符串A第i位和B第j位的相乘结果在结果C的第 i+j+1位(从0开始计数)
class Solution(object): def multiply(self, num1, num2): """ :type num1: str :type num2: str :rtype: str """ ans = [0]*(len(num1)+len(num2)) for i in range(len(num1)-1, -1, -1): for j in range(len(num2)-1, -1, -1): ans[i+j+1] += int(num1[i]) * int(num2[j]) if ans[i+j+1]>=10: ans[i+j] += ans[i+j+1]/10 ans[i+j+1]%=10 ans = map(str, ans) ans = "".join(ans).lstrip("0") return ans if len(ans) else "0"