?LeetCode刷題實(shí)戰(zhàn)537:復(fù)數(shù)乘法
A complex number can be represented as a string on the form "real+imaginaryi" where:
real is the real part and is an integer in the range [-100, 100].
imaginary is the imaginary part and is an integer in the range [-100, 100].
i2 == -1.
Given two complex numbers num1 and num2 as strings, return a string of the complex number that represents their multiplications.
實(shí)部 是一個整數(shù),取值范圍是 [-100, 100]
虛部 也是一個整數(shù),取值范圍是 [-100, 100]
i的平分?== -1
示例? ? ? ? ? ? ? ? ? ? ? ? ?
示例 1:
輸入:num1 = "1+1i", num2 = "1+1i"
輸出:"0+2i"
解釋:(1?+ i) * (1?+ i) = 1?+ i2 + 2?* i = 2i?,你需要將它轉(zhuǎn)換為 0+2i?的形式。
示例 2:
輸入:num1 = "1+-1i", num2 = "1+-1i"
輸出:"0+-2i"
解釋:(1?- i) * (1?- i) = 1?+ i2 - 2?* i = -2i?,你需要將它轉(zhuǎn)換為 0+-2i?的形式。
解題

public?class?Solution?{
????public?String complexNumberMultiply(String a, String b)?{
????????String x[] = a.split("\\+|i");
????????String y[] = b.split("\\+|i");
????????int?a_real = Integer.parseInt(x[0]);
????????int?a_img = Integer.parseInt(x[1]);
????????int?b_real = Integer.parseInt(y[0]);
????????int?b_img = Integer.parseInt(y[1]);
????????return?(a_real * b_real - a_img * b_img) + "+"?+ (a_real * b_img + a_img * b_real) + "i";
????}
}
