43. Multiply Strings leetcode python 2016 new season

Total Accepted: 51879  Total Submissions: 231091  Difficulty: Medium

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.

Show Company Tags
Show Tags
Show Similar Problems
Have you met this question in a real interview? 
Yes
 
No

Discuss


class Solution(object):
    def multiply(self, num1, num2):
        """
        :type num1: str
        :type num2: str
        :rtype: str
        """
        num1_len = len(num1)
        num2_len = len(num2)
        arr = [0 for _ in range(num1_len + num2_len)]
        num1 = num1[::-1]
        num2 = num2[::-1]
        result = []
        for i in range(num1_len):
            for j in range(num2_len):
                arr[i + j] += int(num1[i]) * int(num2[j])
        for arr_i in range(num1_len + num2_len):
            digit = arr[arr_i] % 10
            carry = arr[arr_i] / 10
            if arr_i < num1_len + num2_len - 1:
                arr[arr_i + 1] += carry
            result.append(str(digit))
        result.reverse()
        while result[0] == '0' and len(result) > 1:
            del result[0]
        return "".join(result)


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值