Skip to content

Commit fa27dc5

Browse files
committed
day 24
1 parent 2f551be commit fa27dc5

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
Complex Number Multiplication
3+
=============================
4+
5+
A complex number can be represented as a string on the form "real+imaginaryi" where:
6+
7+
real is the real part and is an integer in the range [-100, 100].
8+
imaginary is the imaginary part and is an integer in the range [-100, 100].
9+
i2 == -1.
10+
Given two complex numbers num1 and num2 as strings, return a string of the complex number that represents their multiplications.
11+
12+
Example 1:
13+
Input: num1 = "1+1i", num2 = "1+1i"
14+
Output: "0+2i"
15+
Explanation: (1 + i) * (1 + i) = 1 + i2 + 2 * i = 2i, and you need convert it to the form of 0+2i.
16+
17+
Example 2:
18+
Input: num1 = "1+-1i", num2 = "1+-1i"
19+
Output: "0+-2i"
20+
Explanation: (1 - i) * (1 - i) = 1 + i2 - 2 * i = -2i, and you need convert it to the form of 0+-2i.
21+
22+
Constraints:
23+
num1 and num2 are valid complex numbers.
24+
*/
25+
26+
class Solution {
27+
public:
28+
string complexNumberMultiply(string a, string b) {
29+
int r1 = stoi(a.substr(0, a.find('+')+1));
30+
int r2 = stoi(b.substr(0, b.find('+')+1));
31+
32+
int c1 = stoi(a.substr(a.find('+')+1));
33+
int c2 = stoi(b.substr(b.find('+')+1));
34+
35+
return to_string(r1*r2 - c1*c2) + "+" + to_string(r1*c2 + r2*c1) + "i";
36+
}
37+
};

Diff for: Leetcode Daily Challenge/August-2021/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,5 @@
2424
| 19. | [Maximum Product of Splitted Binary Tree](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge-2021/615/week-3-august-15th-august-21st/3903/) | [cpp](./19.%20Maximum%20Product%20of%20Splitted%20Binary%20Tree.cpp) |
2525
| 20. | [Valid Sudoku](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge-2021/615/week-3-august-15th-august-21st/3904/) | [cpp](./20.%20Valid%20Sudoku.cpp) |
2626
| 21. | [Sudoku Solver](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge-2021/615/week-3-august-15th-august-21st/3905/) | [cpp](./20Sudoku%20Solver.%20.cpp) |
27-
| 23. | [Two Sum IV - Input is a BST](https://leetcode.com/explore/featured/card/august-leetcoding-challenge-2021/616/week-4-august-22nd-august-28th/3908/) | [cpp](./23Two%20Sum%20IV%20-%20Input%20is%20a%20BST.cpp) |
28-
| 24. | []() | [cpp](./24.cpp) |
27+
| 23. | [Two Sum IV - Input is a BST](https://leetcode.com/explore/featured/card/august-leetcoding-challenge-2021/616/week-4-august-22nd-august-28th/3908/) | [cpp](./23.%20Two%20Sum%20IV%20-%20Input%20is%20a%20BST.cpp) |
28+
| 24. | [Complex Number Multiplication](https://leetcode.com/explore/featured/card/august-leetcoding-challenge-2021/616/week-4-august-22nd-august-28th/3917/) | [cpp](./24.%20Complex%20Number%20Multiplication.cpp) |

0 commit comments

Comments
 (0)