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


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:2つのステップに分かれ、1つは文字列を数字に変換し、1つは数字を複素演算し、結果文字列を返す.
  • デジタル関数getnumber:文字列にプラス記号を付ける前の文字列をrealに整数に変換し、プラス記号を付ける後、i前の文字列をvirtualに整数に変換します.
  • 複素演算関数complexNumberMultiply:getnumber関数をそれぞれ呼び出して2つの文字列に対応する実数と虚数の部分を取得し、複素演算を行い、結果を文字列に変換して返します.

  • 構想2:c++とPythonに分かれている.
  • c+:主に文字列とint型変換の処理に改善があります.stringstream処理を利用した.
  • Python:Python言語の特性を利用してmap関数を用い,文字列にsplit関数を用いてスライスし,変換後のint型を得た.


  • 537.3 C++コード:
    1、構想一コード(3 ms):
    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、構想二コード(3 ms)
    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、構想一コード(32 ms)
    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、構想二コード(32 ms)
    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"