Skip to content

Commit c685fc7

Browse files
authored
Update Complex Number Multiplication.java
1 parent 1c827db commit c685fc7

File tree

1 file changed

+6
-16
lines changed

1 file changed

+6
-16
lines changed
Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,9 @@
11
class Solution {
2-
public String complexNumberMultiply(String a, String b) {
3-
int realPart = 0;
4-
int complexPart = 0;
5-
int[] splitA = helper(a);
6-
int[] splitB = helper(b);
7-
realPart = splitA[0] * splitB[0] - splitA[1] * splitB[1];
8-
complexPart = splitA[0] * splitB[1] + splitA[1] * splitB[0];
9-
return realPart + "+" + complexPart + "i";
10-
}
11-
12-
private int[] helper(String s) {
13-
String[] strs = s.split("\\+");
14-
int[] arr = new int[2];
15-
arr[0] = Integer.parseInt(strs[0]);
16-
arr[1] = Integer.parseInt(strs[1].substring(0, strs[1].indexOf('i')));
17-
return arr;
2+
public String complexNumberMultiply(String num1, String num2) {
3+
int realPartOne = Integer.parseInt(num1.split("\\+")[0]);
4+
int imaginaryPartOne = Integer.parseInt(num1.split("\\+")[1].substring(0, num1.split("\\+")[1].length() - 1));
5+
int realPartTwo = Integer.parseInt(num2.split("\\+")[0]);
6+
int imaginaryPartTwo = Integer.parseInt(num2.split("\\+")[1].substring(0, num2.split("\\+")[1].length() - 1));
7+
return (realPartOne * realPartTwo - imaginaryPartOne * imaginaryPartTwo) + "+" + (realPartOne * imaginaryPartTwo + realPartTwo * imaginaryPartOne) + "i";
188
}
199
}

0 commit comments

Comments
 (0)