小言_互联网的博客

leetcode刷题python之字符串相乘

407人阅读  评论(0)
class Solution:
    def multiply(self, num1: str, num2: str) -> str:
        if num1 == '0' or num2 == '0':
            return '0' 
        num1 = num1[::-1]
        num2 = num2[::-1]
        result = [0] * (len(num1) + len(num2))
        for index1 , i1 in enumerate(num1):
            # n1 = int(i1)
            for index2 , i2 in enumerate(num2):
                # n2 = int(i2)
                sum = result[index1 + index2] + int(i1)*int(i2)
                result[index1 + index2] = sum % 10
                result[index1 + index2 + 1] += sum // 10
                
        res = ''.join([ str(x) for x in result ])[::-1].lstrip('0')
        # res = ''.join(res)
        # res = res[::-1]
        # res = res.lstrip('0')
        return res

转载:https://blog.csdn.net/leileii/article/details/101546823
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场