forked from shijbian/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmultiply-strings.py
More file actions
32 lines (27 loc) · 953 Bytes
/
multiply-strings.py
File metadata and controls
32 lines (27 loc) · 953 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# Time: O(m * n)
# Space: O(m + n)
#
# 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.
#
class Solution:
# @param num1, a string
# @param num2, a string
# @return a string
def multiply(self, num1, num2):
num1, num2 = num1[::-1], num2[::-1]
result = [0 for i in xrange(len(num1) + len(num2))]
for i in xrange(len(num1)):
for j in xrange(len(num2)):
result[i + j] += int(num1[i]) * int(num2[j])
carry, num3 = 0, []
for digit in result:
sum = carry + digit
carry = sum / 10
num3.insert(0, str(sum % 10))
while len(num3) > 1 and num3[0] == "0":
del num3[0]
return ''.join(num3)
if __name__ == "__main__":
print Solution().multiply("123", "1000")