|
1 | 1 | package com.fishercoder.solutions;
|
2 | 2 |
|
| 3 | +import java.util.stream.Stream; |
| 4 | + |
3 | 5 | /**
|
| 6 | + * 537. Complex Number Multiplication |
| 7 | + * |
4 | 8 | * Given two strings representing two complex numbers.
|
5 | 9 |
|
6 | 10 | You need to return a string representing their multiplication. Note i2 = -1 according to the definition.
|
|
21 | 25 | */
|
22 | 26 | public class _537 {
|
23 | 27 |
|
24 |
| - public String complexNumberMultiply(String a, String b) { |
25 |
| - String[] part1And2 = a.split("\\+"); |
26 |
| - String[] part3And4 = b.split("\\+"); |
27 |
| - String product1 = String.valueOf(Integer.parseInt(part1And2[0]) * Integer.parseInt(part3And4[0]));//this is real number multiplication |
28 |
| - String product2 = multiply(part1And2[0], part3And4[1]); |
29 |
| - String product3 = multiply(part3And4[0], part1And2[1]); |
30 |
| - String product4 = multiplyTwoIs(part3And4[1], part1And2[1]); |
31 |
| - String twoISum = sumTwoI(product2, product3); |
32 |
| - String numberValue = String.valueOf(Integer.valueOf(product1) + Integer.valueOf(product4)); |
33 |
| - return numberValue + "+" + twoISum; |
34 |
| - } |
| 28 | + public static class Solution1 { |
| 29 | + public String complexNumberMultiply(String a, String b) { |
| 30 | + String[] part1And2 = a.split("\\+"); |
| 31 | + String[] part3And4 = b.split("\\+"); |
| 32 | + String product1 = String.valueOf(Integer.parseInt(part1And2[0]) * Integer.parseInt(part3And4[0]));//this is real number multiplication |
| 33 | + String product2 = multiply(part1And2[0], part3And4[1]); |
| 34 | + String product3 = multiply(part3And4[0], part1And2[1]); |
| 35 | + String product4 = multiplyTwoIs(part3And4[1], part1And2[1]); |
| 36 | + String twoISum = sumTwoI(product2, product3); |
| 37 | + String numberValue = String.valueOf(Integer.valueOf(product1) + Integer.valueOf(product4)); |
| 38 | + return numberValue + "+" + twoISum; |
| 39 | + } |
35 | 40 |
|
36 |
| - private String sumTwoI(String product2, String product3) { |
37 |
| - int number2 = Integer.parseInt(product2.substring(0, product2.length() - 1)); |
38 |
| - int number3 = Integer.parseInt(product3.substring(0, product3.length() - 1)); |
39 |
| - return String.valueOf(number2 + number3) + "i"; |
40 |
| - } |
| 41 | + private String sumTwoI(String product2, String product3) { |
| 42 | + int number2 = Integer.parseInt(product2.substring(0, product2.length() - 1)); |
| 43 | + int number3 = Integer.parseInt(product3.substring(0, product3.length() - 1)); |
| 44 | + return String.valueOf(number2 + number3) + "i"; |
| 45 | + } |
41 | 46 |
|
42 |
| - private String multiplyTwoIs(String p, String q) { |
43 |
| - int number1 = Integer.parseInt(p.substring(0, p.length() - 1)); |
44 |
| - int number2 = Integer.parseInt(q.substring(0, q.length() - 1)); |
45 |
| - int numberProduct = number1 * number2; |
46 |
| - return String.valueOf(-numberProduct); |
47 |
| - } |
| 47 | + private String multiplyTwoIs(String p, String q) { |
| 48 | + int number1 = Integer.parseInt(p.substring(0, p.length() - 1)); |
| 49 | + int number2 = Integer.parseInt(q.substring(0, q.length() - 1)); |
| 50 | + int numberProduct = number1 * number2; |
| 51 | + return String.valueOf(-numberProduct); |
| 52 | + } |
48 | 53 |
|
49 |
| - private String multiply(String p, String withI) { |
50 |
| - int numberPart = Integer.parseInt(withI.substring(0, withI.length() - 1)); |
51 |
| - return String.valueOf(numberPart * Integer.valueOf(p)) + "i"; |
| 54 | + private String multiply(String p, String withI) { |
| 55 | + int numberPart = Integer.parseInt(withI.substring(0, withI.length() - 1)); |
| 56 | + return String.valueOf(numberPart * Integer.valueOf(p)) + "i"; |
| 57 | + } |
52 | 58 | }
|
53 | 59 |
|
| 60 | + public static class Solution2 { |
| 61 | + /** |
| 62 | + * (a + bi) * (c + di) could become (ac - bd) + (ad + bc)*i |
| 63 | + * Thus, we have the following function |
| 64 | + */ |
| 65 | + public String complexNumberMultiply(String a, String b) { |
| 66 | + int[] coefficients1 = Stream.of(a.split("\\+|i")).mapToInt(Integer::parseInt).toArray(); |
| 67 | + int[] coefficients2 = Stream.of(b.split("\\+|i")).mapToInt(Integer::parseInt).toArray(); |
| 68 | + return (coefficients1[0] * coefficients2[0] - coefficients1[1] * coefficients2[1]) + "+" |
| 69 | + + (coefficients1[0] * coefficients2[1] + coefficients1[1] * coefficients2[0] + "i"); |
| 70 | + } |
| 71 | + } |
54 | 72 |
|
55 | 73 | }
|
0 commit comments