0537-复数乘法

Raphael Liu Lv10

复数
可以用字符串表示,遵循 " **实部** + **虚部** i" 的形式,并满足下述条件:

  • 实部 是一个整数,取值范围是 [-100, 100]
  • 虚部 也是一个整数,取值范围是 [-100, 100]
  • i2 == -1

给你两个字符串表示的复数 num1num2 ,请你遵循复数表示形式,返回表示它们乘积的字符串。

示例 1:

**输入:** num1 = "1+1i", num2 = "1+1i"
**输出:** "0+2i"
**解释:** (1 + i) * (1 + i) = 1 + i2 + 2 * i = 2i ,你需要将它转换为 0+2i 的形式。

示例 2:

**输入:** num1 = "1+-1i", num2 = "1+-1i"
**输出:** "0+-2i"
**解释:** (1 - i) * (1 - i) = 1 + i2 - 2 * i = -2i ,你需要将它转换为 0+-2i 的形式。 

提示:

  • num1num2 都是有效的复数表示。

方法一:模拟

复数可以写成 a + b\mathrm{i 的形式,其中 a,b \in \mathbb{R,a 是实部,b 是虚部,\mathrm{i 是虚数单位,\mathrm{i}^2 = -1。

对于给定的两个复数 num}_1 和 num}_2,首先分别得到两个复数的实部和虚部,然后计算两个复数的乘法。用 real}_1 和 imag}_1 分别表示 num}_1 的实部和虚部,用 real}_2 和 imag}_2 分别表示 num}_2 的实部和虚部,则两个复数的乘法计算如下:

\begin{aligned}
\quad~ &(\textit{real}_1 + \textit{imag}_1 \times \mathrm{i}) \times (\textit{real}_2 + \textit{imag}_2 \times \mathrm{i}) \
= &
\textit{real}_1 \times \textit{real}_2 + \textit{real}_1 \times \textit{imag}_2 \times \mathrm{i} + \textit{imag}_1 \times \textit{real}_2 \times \mathrm{i} + \textit{imag}_1 \times \textit{imag}_2 \times \mathrm{i}^2 \
= &\textit{real}_1 \times \textit{real}_2 + \textit{real}_1 \times \textit{imag}_2 \times \mathrm{i} + \textit{imag}_1 \times \textit{real}_2 \times \mathrm{i} - \textit{imag}_1 \times \textit{imag}_2 \
= &
(\textit{real}_1 \times \textit{real}_2 - \textit{imag}_1 \times \textit{imag}_2) + (\textit{real}_1 \times \textit{imag}_2 + \textit{imag}_1 \times \textit{real}_2) \times \mathrm{i}
\end{aligned}

得到两个复数的乘积之后,将乘积转换成复数格式的字符串并返回。

[sol1-Python3]
1
2
3
4
5
class Solution:
def complexNumberMultiply(self, num1: str, num2: str) -> str:
real1, imag1 = map(int, num1[:-1].split('+'))
real2, imag2 = map(int, num2[:-1].split('+'))
return f'{real1 * real2 - imag1 * imag2}+{real1 * imag2 + imag1 * real2}i'
[sol1-Java]
1
2
3
4
5
6
7
8
9
10
11
class Solution {
public String complexNumberMultiply(String num1, String num2) {
String[] complex1 = num1.split("\\+|i");
String[] complex2 = num2.split("\\+|i");
int real1 = Integer.parseInt(complex1[0]);
int imag1 = Integer.parseInt(complex1[1]);
int real2 = Integer.parseInt(complex2[0]);
int imag2 = Integer.parseInt(complex2[1]);
return String.format("%d+%di", real1 * real2 - imag1 * imag2, real1 * imag2 + imag1 * real2);
}
}
[sol1-C#]
1
2
3
4
5
6
7
8
9
10
11
public class Solution {
public string ComplexNumberMultiply(string num1, string num2) {
string[] complex1 = num1.Split(new char[2]{'+','i'});
string[] complex2 = num2.Split(new char[2]{'+','i'});
int real1 = int.Parse(complex1[0]);
int imag1 = int.Parse(complex1[1]);
int real2 = int.Parse(complex2[0]);
int imag2 = int.Parse(complex2[1]);
return string.Format("{0}+{1}i", real1 * real2 - imag1 * imag2, real1 * imag2 + imag1 * real2);
}
}
[sol1-C++]
1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution {
public:
string complexNumberMultiply(string num1, string num2) {
regex re("\\+|i");
vector<string> complex1(sregex_token_iterator(num1.begin(), num1.end(), re, -1), std::sregex_token_iterator());
vector<string> complex2(sregex_token_iterator(num2.begin(), num2.end(), re, -1), std::sregex_token_iterator());
int real1 = stoi(complex1[0]);
int imag1 = stoi(complex1[1]);
int real2 = stoi(complex2[0]);
int imag2 = stoi(complex2[1]);
return to_string(real1 * real2 - imag1 * imag2) + "+" + to_string(real1 * imag2 + imag1 * real2) + "i";
}
};
[sol1-C]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
bool parseComplexNumber(const char * num, int * real, int * image) {
char *token = strtok(num, "+");
*real = atoi(token);
token = strtok(NULL, "i");
*image = atoi(token);
return true;
};

char * complexNumberMultiply(char * num1, char * num2){
int real1 = 0, imag1 = 0;
int real2 = 0, imag2 = 0;
char * res = (char *)malloc(sizeof(char) * 20);
parseComplexNumber(num1, &real1, &imag1);
parseComplexNumber(num2, &real2, &imag2);
snprintf(res, 20, "%d+%di", real1 * real2 - imag1 * imag2, real1 * imag2 + imag1 * real2);
return res;
}
[sol1-JavaScript]
1
2
3
4
5
6
7
8
9
var complexNumberMultiply = function(num1, num2) {
const complex1 = [num1.split("+")[0], num1.split("+")[1].split("i")[0]];
const complex2 = [num2.split("+")[0], num2.split("+")[1].split("i")[0]];
const real1 = parseInt(complex1[0]);
const imag1 = parseInt(complex1[1]);
const real2 = parseInt(complex2[0]);
const imag2 = parseInt(complex2[1]);
return '' + real1 * real2 - imag1 * imag2 + '+' + (real1 * imag2 + imag1 * real2) + 'i';
};
[sol1-Golang]
1
2
3
4
5
6
7
8
9
10
11
12
func parseComplexNumber(num string) (real, imag int) {
i := strings.IndexByte(num, '+')
real, _ = strconv.Atoi(num[:i])
imag, _ = strconv.Atoi(num[i+1 : len(num)-1])
return
}

func complexNumberMultiply(num1, num2 string) string {
real1, imag1 := parseComplexNumber(num1)
real2, imag2 := parseComplexNumber(num2)
return fmt.Sprintf("%d+%di", real1*real2-imag1*imag2, real1*imag2+imag1*real2)
}

复杂度分析

  • 时间复杂度:O(1)。由于两个复数字符串的长度都很小,因此可以将字符串处理的时间视为常数。

  • 空间复杂度:O(1)。

 Comments
On this page
0537-复数乘法