File tree Expand file tree Collapse file tree 1 file changed +6
-16
lines changed Expand file tree Collapse file tree 1 file changed +6
-16
lines changed Original file line number Diff line number Diff line change 1
1
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" ;
18
8
}
19
9
}
You can’t perform that action at this time.
0 commit comments