-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmultiply_strings.py
More file actions
97 lines (69 loc) · 1.97 KB
/
multiply_strings.py
File metadata and controls
97 lines (69 loc) · 1.97 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
"""
https://leetcode.com/problems/multiply-strings
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.
"""
def multiply(num1: str, num2: str) -> str:
res = "0"
if "0" in [num1, num2]:
return "0"
for i, num in enumerate(num1[::-1]):
curr = multiplyOneNum(num, num2, i)
res = add(res, curr)
return res
def multiplyOneNum(num: str, num2: str, placeValue: int) -> str:
res = ""
num2 = num2[::-1]
carriedNum = 0
for i in range(len(num2)):
curr = int(num) * int(num2[i])
curr += carriedNum
carriedNum = 0
while curr >= 10:
curr -= 10
carriedNum += 1
res += f"{curr}"
if carriedNum > 0:
res += f"{carriedNum}"
res = res[::-1]
while placeValue > 0:
res += "0"
placeValue -= 1
return res
def add(num1: str, num2: str) -> str:
if num1 == "0" or num2 == "0":
return num2 if num1 == "0" else num1
res = ""
i = 0
num1 = num1[::-1]
num2 = num2[::-1]
carry = False
while i < len(num1) or i < len(num2):
curr = 0
if carry:
curr += 1
carry = False
if i > len(num1) - 1:
curr += int(num2[i])
elif i > len(num2) - 1:
curr += int(num1[i])
else:
curr += int(num1[i]) + int(num2[i])
if curr >= 10:
curr -= 10
carry = True
i += 1
res += f"{curr}"
if carry:
res += "1"
return res[::-1]