C++ Solution


  • 0
    Y
    class Solution {
    public:
        string complexNumberMultiply(string a, string b) {
            
            int ra, ia, rb, ib;
            for (int i=0; i<a.length(); i++) {
                if (a[i] == '+') {
                    ra = stoi(a.substr(0, i));
                    ia = stoi(a.substr(i+1, a.length()-i-2));
                    break;
                }
            }
            for (int i=0; i<b.length(); i++) {
                if (b[i] == '+') {
                    rb = stoi(b.substr(0, i));
                    ib = stoi(b.substr(i+1, b.length()-i-2));
                    break;
                }
            }
            int rp, ip;
            rp = ra*rb-ia*ib;
            ip = ra*ib+ia*rb;
            return to_string(rp)+"+"+to_string(ip)+"i";
            
        }
    };
    

Log in to reply
 

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