537. Complex Number Multiplication
- Complex Number Multiplicationpython solution
题目描述
Given two strings representing two complex numbers.
You need to return a string representing their multiplication. Note i*i = -1 according to the definition.
解析
本木很好理解,就当做来认识一下Python的基本函数吧。
replace('i','j')
将字符串中的i替换成j
eval()
将字符串对象转化为可操作的对象
complex()
进行复数操作
// An highlighted block
class Solution:
def complexNumberMultiply(self, a: str, b: str) -> str:
x=str(a)
y=str(b)
x=x.replace('i','j')
y=y.replace('i','j')
ret=complex(eval(x))*complex(eval(y))
return (str(int(ret.real))+'+'+str(int(ret.imag))+'i')
Reference
https://leetcode.com/problems/complex-number-multiplication/discuss/370659/simple-python