C++ 3ms solution using substr, find and stoi


  • 0
    F
    string complexNumberMultiply(string a, string b) {
        int a1, b1, a2, b2, a3, b3;
        a1 = stoi(a.substr(0, a.find("+")));
        b1 = stoi(a.substr(a.find("+")+1, a.size()-1));
        a2 = stoi(b.substr(0, b.find("+")));
        b2 = stoi(b.substr(b.find("+")+1, b.size()-1));
        a3 = a1*a2 - b1*b2;
        b3 = a1*b2 + a2*b1;
        return to_string(a3) + "+" + to_string(b3) + "i";
    }

Log in to reply
 

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