LeetCode 537 Complex Number Multiplication (Python)

Posted by 小明MaxMing on August 24, 2021

题目

A complex number can be represented as a string on the form “real+imaginaryi” where:

  • real is the real part and is an integer in the range [-100, 100].
  • imaginary is the imaginary part and is an integer in the range [-100, 100].
  • i^2 == -1. Given two complex numbers num1 and num2 as strings, return a string of the complex number that represents their multiplications.

解题思路

先拿到两个数的实部和虚部,求出结果

代码

class Solution:
    def complexNumberMultiply(self, num1: str, num2: str) -> str:
        a1, b1 = map(int, num1[:-1].split('+'))
        a2, b2 = map(int, num2[:-1].split('+'))
        return '%d+%di' % (a1 * a2 - b1 * b2, a1 * b2 + a2 * b1)

视频讲解 YouTube<--欢迎点击订阅

视频讲解 bilibili<--欢迎点击订阅