c++ solution


  • 0
    W
    class Solution {
        void parse(const string& c, int& real, int& imaginary) {
            bool realneg=false;
            bool realstart=false;
            int i=0;
            char cc=c.at(i);
            while (cc!='+') {
                if (!realstart && cc=='-') realneg=true;
                else if (!realstart && isdigit(cc)) {
                    realstart=true;
                    real=cc-'0';
                }
                else if (realstart && isdigit(cc)) {
                    real *= 10;
                    real += (cc-'0');
                }
                i+=1;
                cc=c.at(i);
            }
            if (realneg) real *= -1;
            i+=1; // skip '+'
            bool imagneg=false;
            bool imagstart=false;
            cc=c.at(i);
            while (cc!='i') {
                if (!imagstart && cc=='-') imagneg=true;
                else if (!imagstart && isdigit(cc)) {
                    imagstart=true;
                    imaginary=cc-'0';
                }
                else if (imagstart && isdigit(cc)) {
                    imaginary *= 10;
                    imaginary += (cc-'0');
                }
                i+=1;
                cc=c.at(i);
            }
            if (imagneg) imaginary *= -1;
        }
    public:
        string complexNumberMultiply(string a, string b) {
            int areal,aimag,breal,bimag;
            parse(a,areal,aimag);
            parse(b,breal,bimag);
            int real = areal * breal + aimag * bimag * -1;
            int imag = areal * bimag + breal * aimag;
            stringstream ss;
            ss << real << '+' << imag << 'i';
            return ss.str();
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.