Scribbling

LeetCode: 43. Multiply Strings 본문

Computer Science/Coding Test

LeetCode: 43. Multiply Strings

focalpoint 2021. 9. 13. 22:19
class Solution:
    def multiply(self, num1: str, num2: str) -> str:
        if num1 == '0' or num2 == '0':
            return '0'
        
        ret = [0] * (len(num1) + len(num2))
        for i in range(len(num1)-1, -1, -1):
            carry = 0
            for j in range(len(num2)-1, -1, -1):
                temp = ret[i+j+1] + int(num1[i]) * int(num2[j]) + carry
                carry = temp // 10
                ret[i+j+1] = temp % 10
            ret[i] = carry
        ret = ''.join(map(str, ret))
        return ret.lstrip('0')

'Computer Science > Coding Test' 카테고리의 다른 글

LeetCode: 51. N-Queens  (0) 2021.09.14
LeetCode: 10. Regular Expression Matching  (0) 2021.09.14
LeetCode: 45. Jump Game II  (0) 2021.09.13
LeetCode: 43. Multiply Strings  (0) 2021.09.13
LeetCode: 42. Trapping Rain Water  (0) 2021.09.12