-
-
Notifications
You must be signed in to change notification settings - Fork 300
/
537.py
39 lines (38 loc) · 1.43 KB
/
537.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
__________________________________________________________________________________________________
sample 24 ms submission
class Solution:
def complexNumberMultiply(self, a: str, b: str) -> str:
A = [int(x) for x in a.replace('i','').split('+')]
B = [int(x) for x in b.replace('i','').split('+')]
return str(A[0]*B[0]-A[1]*B[1])+'+'+str(A[0]*B[1]+A[1]*B[0])+'i'
__________________________________________________________________________________________________
sample 13124 kb submission
class Solution:
def getrc(self, strs):
val = ''
r, c = 0, 0
positive = True
for char in strs:
if char == '-':
positive = False
elif char != '+' and char != 'i':
val += char
else:
val = int(val)
if not positive: val = -val
if char == '+': r = val
else: c = val
val = ''
positive = True
return (r, c)
def complexNumberMultiply(self, a: str, b: str) -> str:
ra, ca = self.getrc(a)
rb, cb = self.getrc(b)
r = ra*rb-ca*cb
c = ra*cb+rb*ca
if r >= 0: r = str(r)
else: r = '-' + str(-r)
if c >= 0: c = str(c)
else: c = '-' + str(-c)
return r + '+' + c + 'i'
__________________________________________________________________________________________________