-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0043_multiply_strings.py
More file actions
57 lines (44 loc) · 1.48 KB
/
0043_multiply_strings.py
File metadata and controls
57 lines (44 loc) · 1.48 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
'''
43. Multiply Strings
Medium
Given two non-negative integers num1 and num2 represented as strings,
return the product of num1 and num2, also represented as a string.
Note: You must not use any built-in BigInteger library or convert
the inputs to integer directly.
Example 1:
Input: num1 = "2", num2 = "3"
Output: "6"
Example 2:
Input: num1 = "123", num2 = "456"
Output: "56088"
Constraints:
1 <= num1.length, num2.length <= 200
num1 and num2 consist of digits only.
Both num1 and num2 do not contain any leading zero, except the number 0 itself.
'''
class Solution:
def multiply(self, num1: str, num2: str) -> str:
right2 = len(num2) - 1
result = 0
while right2 >= 0:
additional = 0
preresult = ""
right1 = len(num1) - 1
while right1 >= 0 or additional != 0:
x = 0
if right1 >= 0:
x = int(num2[right2]) * int(num1[right1])
right1 -= 1
x = x + additional
preresult = str(x % 10) + preresult
additional = x // 10
result = result + int(preresult) * (10 ** (len(num2) - 1 - right2))
right2 -= 1
return str(result)
sol = Solution()
assert sol.multiply("123", "456") == "56088"
assert sol.multiply("11", "11") == "121"
assert sol.multiply("125698", "0") == "0"
assert sol.multiply("0", "0") == "0"
assert sol.multiply("0", "125") == "0"
assert sol.multiply("7", "12") == "84"