Medium
A complex number can be represented as a string on the form "**real**+**imaginary**i"
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.
Example 1:
Input: num1 = “1+1i”, num2 = “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: num1 = “1+-1i”, num2 = “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.
Constraints:
num1
and num2
are valid complex numbers.public class Solution {
public String complexNumberMultiply(String num1, String num2) {
int countReal;
int countImagine;
int[] arr1 = new int[2];
int[] arr2 = new int[2];
arr1[0] = Integer.parseInt(num1.substring(0, num1.indexOf("+")));
arr1[1] = Integer.parseInt(num1.substring(num1.indexOf("+") + 1, num1.length() - 1));
arr2[0] = Integer.parseInt(num2.substring(0, num2.indexOf("+")));
arr2[1] = Integer.parseInt(num2.substring(num2.indexOf("+") + 1, num2.length() - 1));
countReal = arr1[0] * arr2[0] - arr1[1] * arr2[1];
countImagine = arr1[0] * arr2[1] + arr1[1] * arr2[0];
return countReal + "+" + countImagine + "i";
}
}