class Solution { public: string complexNumberMultiply(string num1, string num2) { int shi1 = 0, shi2 = 0; int xu1 = 0, xu2 = 0; shi1 = getShi(num1); shi2 = getShi(num2); xu1 = getXu(num1); xu2 = getXu(num2); string ans = ""; int shi = shi1 * shi2 - xu1 * xu2; int xu = shi1 * xu2 + xu1 * shi2; ans += toString(shi) + "+"; ans += toString(xu) + "i"; return ans; } int getShi(string s) { int len = s.size(); int ret = 0; int fu = 1; for(int i = 0; i < len; i ++) { if(s[i] == '-') { fu = -1; continue; } if(s[i] == '+') break; ret = ret * 10 + s[i] - '0'; } return ret * fu; } int getXu(string s) { int ret = 0; int len = s.size(); int flag = 0; int fu = 1; for(int i = 0; i < len; i ++) { if(s[i] == '+') flag = 1; if(s[i] == '-' && i != 0) { fu = -1; continue; } if(s[i] == 'i') break; if(flag && isdigit(s[i])) { ret = ret * 10 + s[i] - '0'; } } return ret * fu; } string toString(int n) { if(n == 0) { string t = "0"; return t; } string ret = ""; int fu = 0; if(n < 0) fu = 1, n *= -1; while(n) { int t = n % 10; n /= 10; ret.push_back('0' + t); } reverse(ret.begin(),ret.end()); if(fu == 1) { ret = "-" + ret; } return ret; } };
文章知识点与官方知识档案匹配,可进一步学习相关知识
算法技能树leetcode-动态规划22-括号生成8242 人正在系统学习中