The Complex Number Multiplication Function
- 时间:2020-09-18 17:26:09
- 分类:网络文摘
- 阅读:146 次
Given two strings representing two complex numbers. You need to return a string representing their multiplication. Note i2 = -1 according to the definition.
Example 1:
Input: “1+1i”, “1+1i”
Output: “0+2i”
Explanation: (1 + i) * (1 + i) = 1 + i2 + 2 * i = 2i, and you need convert it to the form of 0+2i.Example 2:
Input: “1+-1i”, “1+-1i”
Output: “0+-2i”Explanation: (1 – i) * (1 – i) = 1 + i2 – 2 * i = -2i, and you need convert it to the form of 0+-2i.
Note:
The input strings will not have extra blank.
The input strings will be given in the form of a+bi, where the integer a and b will both belong to the range of [-100, 100]. And the output should be also in this form.
The Javascript Function to Compute the Complex Number Multiplication
In Javascript, we can use the split function, which allows us to separate the complex number string into two parts: the real and the imaginary. We can use the slice(0, -1) to remove the last character ‘i’ from the imaginary. Then, we just need to compute the real and imaginary of the result complex number respectively using simple math multiplication law.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | /** * @param {string} a * @param {string} b * @return {string} */ var complexNumberMultiply = function(a, b) { const process = (x) => { x = x.split('+'); const a = x[0]; const b = x[1].slice(0, -1); return [a, b]; } const aa = process(a); const bb = process(b); const xx = aa[0] * bb[0] - aa[1] * bb[1]; const yy = aa[0] * bb[1] + aa[1] * bb[0]; return `${xx}+${yy}i`; }; |
/**
* @param {string} a
* @param {string} b
* @return {string}
*/
var complexNumberMultiply = function(a, b) {
const process = (x) => {
x = x.split('+');
const a = x[0];
const b = x[1].slice(0, -1);
return [a, b];
}
const aa = process(a);
const bb = process(b);
const xx = aa[0] * bb[0] - aa[1] * bb[1];
const yy = aa[0] * bb[1] + aa[1] * bb[0];
return `${xx}+${yy}i`;
};The time and space complexity is both O(1) constant – assuming spliting a complex number string is trivial.
The Complex Number Multiplication in C++
In C++, we can use the sscanf function to extract values from the string by analyzing the string using format/templates. Then the rest are similar.
1 2 3 4 5 6 7 8 9 10 11 | class Solution { public: string complexNumberMultiply(string a, string b) { int a1, a2, b1, b2; sscanf(a.c_str(), "%d+%di", &a1, &a2); sscanf(b.c_str(), "%d+%di", &b1, &b2); int xx = a1 * b1 - a2 * b2; int yy = a1 * b2 + a2 * b1; return std::to_string(xx) + "+" + std::to_string(yy) + "i"; } }; |
class Solution {
public:
string complexNumberMultiply(string a, string b) {
int a1, a2, b1, b2;
sscanf(a.c_str(), "%d+%di", &a1, &a2);
sscanf(b.c_str(), "%d+%di", &b1, &b2);
int xx = a1 * b1 - a2 * b2;
int yy = a1 * b2 + a2 * b1;
return std::to_string(xx) + "+" + std::to_string(yy) + "i";
}
};–EOF (The Ultimate Computing & Technology Blog) —
推荐阅读:十大“垃圾”食品在你家的餐桌上吗? 让你的孩子远离“三无”劣质小零食 网络传闻中的“致癌食物”是否真致癌 冬季食补:最佳的羊肉吃法,你知道吗 鸡汤虽营养丰富,但这几种病人不要喝 饮食健康与胃病食疗(一):胃病患者饮食注意事项 饮食健康与胃病食疗(二):慢性胃炎的饮食调理 饮食健康与胃病食疗(三):这样饮食降低胃癌风险 冬至时节,常吃这几种传统美食可补阳、防寒! 只有这样吃大蒜才能杀菌防癌,以前你吃对了吗
- 评论列表
-
- 添加评论