1185. Complex Number Multiplication
class Solution {
public:
/**
* @param a: a string
* @param b: a string
* @return: a string representing their multiplication
*/
string complexNumberMultiply(string &a, string &b) {
// Write your code here
int sizeA = a.size();
int i = 0;
while(i < sizeA && a[i] != '+'){
i++;
}
int ai = stoi(a.substr(0, i));
int aq = stoi(a.substr(i + 1, sizeA - 1 - (i + 1)));
int size = b.size();
i = 0;
while(i < size && b[i] != '+'){
i++;
}
int bi = stoi(b.substr(0, i));
int bq = stoi(b.substr(i + 1, size - 1 - (i + 1)));
int newI = ai * bi - aq * bq;
int newQ = ai * bq + aq * bi;
string res = to_string(newI) + "+";
res += to_string(newQ) + "i";
return res;
}
};
Comments
Post a Comment