-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprob_43.py
53 lines (42 loc) · 1.4 KB
/
prob_43.py
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
# 43. Multiply Strings
# https://leetcode.com/problems/multiply-strings/
class Solution(object):
def multiply(self, num1, num2):
"""
:type num1: str
:type num2: str
:rtype: str
"""
res = [0 for i in range(len(num1) + len(num2))]
for i, n1 in enumerate(reversed(num1)):
for j, n2 in enumerate(reversed(num2)):
res[i+j] += int(n1) * int(n2)
res[i+j+1] += res[i+j]/10
res[i+j] %= 10
res_str = ''
first_non_zero = False
for i, v in enumerate(reversed(res)):
if v != 0:
first_non_zero = True
elif not first_non_zero:
continue
res_str += str(v)
res_str = '0' if len(res_str) == 0 else res_str
return res_str
class classname(object):
pass
class Solution_0(object):
def multiply(self, num1, num2):
"""
:type num1: str
:type num2: str
:rtype: str
"""
res = [0 for i in range(len(num1) + len(num2))]
for i, n1 in enumerate(reversed(num1)):
for j, n2 in enumerate(reversed(num2)):
res[i+j] += int(n1) * int(n2)
res[i+j+1] += res[i+j]/10
res[i+j] %= 10
while len(res) > 1 and res[-1] == 0: res.pop()
return ''.join( map(str,res[::-1]) )