【leetcode】537. Complex Number Multiplication(Python & C++)

来源:互联网 发布:linux关机 编辑:程序博客网 时间:2024/06/06 10:55

537. Complex Number Multiplication

题目链接

537.1 题目描述:

Given two strings representing two complex numbers.

You need to return a string representing their multiplication. Note i2 = -1 according to the definition.

Example 1:

Input: “1+1i”, “1+1i”
Output: “0+2i”
Explanation: (1 + i) * (1 + i) = 1 + i2 + 2 * i = 2i, and you need convert it to the form of 0+2i.

Example 2:

Input: “1+-1i”, “1+-1i”
Output: “0+-2i”
Explanation: (1 - i) * (1 - i) = 1 + i2 - 2 * i = -2i, and you need convert it to the form of 0+-2i.

Note:

The input strings will not have extra blank.
The input strings will be given in the form of a+bi, where the integer a and b will both belong to the range of [-100, 100]. And the output should be also in this form.

537.2 解题思路:

  1. 思路一:分为两步,一步将字符串转化为数字,一步将数字进行复数运算,并返回结果字符串。

    • 获取数字函数getnumber:将字符串中加号之前的字符串转化为整型赋给real,将加号之后,i之前的字符串转化为整型赋给virtual。

    • 复数运算函数complexNumberMultiply:分别调用getnumber函数获取两个字符串对应的实数和虚数部分,然后进行复数运算,将结果转化为字符串并返回。

  2. 思路二:分为c++和Python。

    • c++:主要是处理字符串与int型转换上有改进。利用了stringstream处理。

    • Python:利用了Python语言的特性,使用了map函数,并对字符串使用split函数进行分片,得到转换后的int型。

537.3 C++代码:

1、思路一代码(3ms):

class Solution138 {public:    void getnumber(string s, int &real, int &ivirtual)    {        string sreal = "0";        string svirtual = "0";        for (int i = 0; i < s.length();i++)        {            if (s[i]=='+')            {                sreal = s.substr(0, i);                svirtual = s.substr(i + 1, s.length() - i - 2);            }        }        real = atoi(sreal.c_str());        ivirtual = atoi(svirtual.c_str());    }    string complexNumberMultiply(string a, string b) {        int a_real, a_ivirtual, b_real, b_ivirtual;        getnumber(a, a_real, a_ivirtual);        getnumber(b, b_real, b_ivirtual);        int real = a_real*b_real - a_ivirtual*b_ivirtual;        int ivirtual = a_ivirtual * b_real + b_ivirtual * a_real;        string result = to_string(real) + "+" + to_string(ivirtual) + "i";        return result;    }};

2、思路二代码(3ms)

class Solution138_1 {public:    string complexNumberMultiply(string a, string b) {        int ra, ia, rb, ib;        char buff;        stringstream aa(a), bb(b), ans;        aa >> ra >> buff >> ia >> buff;        bb >> rb >> buff >> ib >> buff;        ans << ra*rb - ia*ib << "+" << ia*rb + ib*ra << "i";        return ans.str();    }};

537.4 Python代码:

1、思路一代码(32ms)

class Solution(object):    def complexNumberMultiply(self, a, b):        """        :type a: str        :type b: str        :rtype: str        """        def getnumber(s):            sreal="0"            svirtual="0"            for i in range(len(s)):                if s[i]=='+':                    sreal=s[0:i]                    svirtual=s[i+1:len(s)-1]            return int(sreal),int(svirtual)        a_real,a_virtual=getnumber(a)        b_real,b_virtual=getnumber(b)        real=a_real*b_real-a_virtual*b_virtual        virtual=a_virtual*b_real+b_virtual*a_real        result=str(real)+"+"+str(virtual)+"i"        return result

2、思路二代码(32ms)

class Solution1(object):    def complexNumberMultiply(self, a, b):        """        :type a: str        :type b: str        :rtype: str        """        ra,ia=map(int,a[0:-1].split('+'))        rb,ib=map(int,b[0:-1].split('+'))        return str(ra*rb-ia*ib)+"+"+str(ia*rb+ib*ra)+"i"

原创粉丝点击