Simple C++ 3ms solution


  • 0
    M
    string complexNumberMultiply(string a, string b) {
            vector<int> a1=convert(a);
            vector<int> b1=convert(b);
            string res=to_string(a1[0]*b1[0]-a1[1]*b1[1])+"+"+to_string(a1[0]*b1[1]+a1[1]*b1[0])+"i";
            return res;
        }
        
        vector<int> convert(string a){
            vector<int> res;
            string temp;
            for(int i=0;i<a.size();i++) {
                if(a[i]=='+') {
                    res.push_back(stoi(temp));
                    temp.clear();
                }
                else if(a[i]=='i') res.push_back(stoi(temp));
                else temp.append(1, a[i]);
            }
            return res;
        }

Log in to reply
 

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