-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
18 lines (16 loc) · 771 Bytes
/
Solution.java
File metadata and controls
18 lines (16 loc) · 771 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Solution {
public String complexNumberMultiply(String a, String b) {
java.util.regex.Pattern pattern = java.util.regex.Pattern.compile("(.*)\\+(.*)i");
java.util.regex.Matcher matcher1 = pattern.matcher(a),
matcher2 = pattern.matcher(b);
matcher1.find();
matcher2.find();
int real1 = Integer.valueOf(matcher1.group(1)),
imaginary1 = Integer.valueOf(matcher1.group(2)),
real2 = Integer.valueOf(matcher2.group(1)),
imaginary2 = Integer.valueOf(matcher2.group(2));
int real = real1 * real2 - imaginary1 * imaginary2,
imaginary = real1 * imaginary2 + real2 * imaginary1;
return String.format("%d+%di", real, imaginary);
}
}