Skip to content

Commit ce8dd05

Browse files
committed
feat: add solutions to lc problem: No.0537
No.0537.Complex Number Multiplication
1 parent d6cecfd commit ce8dd05

File tree

6 files changed

+175
-54
lines changed

6 files changed

+175
-54
lines changed

solution/0500-0599/0537.Complex Number Multiplication/README.md

Lines changed: 71 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,53 +6,108 @@
66

77
<!-- 这里写题目描述 -->
88

9-
<p>给定两个表示<a href="https://baike.baidu.com/item/%E5%A4%8D%E6%95%B0/254365?fr=aladdin">复数</a>的字符串。</p>
9+
<p><a href="https://baike.baidu.com/item/%E5%A4%8D%E6%95%B0/254365?fr=aladdin" target="_blank">复数</a> 可以用字符串表示,遵循 <code>"<strong>实部</strong>+<strong>虚部</strong>i"</code> 的形式,并满足下述条件:</p>
1010

11-
<p>返回表示它们乘积的字符串。注意,根据定义 i<sup>2</sup> = -1 。</p>
11+
<ul>
12+
<li><code>实部</code> 是一个整数,取值范围是 <code>[-100, 100]</code></li>
13+
<li><code>虚部</code> 也是一个整数,取值范围是 <code>[-100, 100]</code></li>
14+
<li><code>i<sup>2</sup> == -1</code></li>
15+
</ul>
1216

13-
<p><strong>示例 1:</strong></p>
17+
<p>给你两个字符串表示的复数 <code>num1</code> 和 <code>num2</code> ,请你遵循复数表示形式,返回表示它们乘积的字符串。</p>
18+
19+
<p> </p>
20+
21+
<p><strong>示例 1:</strong></p>
1422

1523
<pre>
16-
<strong>输入:</strong> &quot;1+1i&quot;, &quot;1+1i&quot;
17-
<strong>输出:</strong> &quot;0+2i&quot;
18-
<strong>解释:</strong> (1 + i) * (1 + i) = 1 + i<sup>2</sup> + 2 * i = 2i ,你需要将它转换为 0+2i 的形式。
24+
<strong>输入</strong>num1 = "1+1i", num2 = "1+1i"
25+
<strong>输出</strong>"0+2i"
26+
<strong>解释</strong>(1 + i) * (1 + i) = 1 + i2 + 2 * i = 2i ,你需要将它转换为 0+2i 的形式。
1927
</pre>
2028

21-
<p><strong>示例 2:</strong></p>
29+
<p><strong>示例 2</strong></p>
2230

2331
<pre>
24-
<strong>输入:</strong> &quot;1+-1i&quot;, &quot;1+-1i&quot;
25-
<strong>输出:</strong> &quot;0+-2i&quot;
26-
<strong>解释:</strong> (1 - i) * (1 - i) = 1 + i<sup>2</sup> - 2 * i = -2i ,你需要将它转换为 0+-2i 的形式。
32+
<strong>输入</strong>num1 = "1+-1i", num2 = "1+-1i"
33+
<strong>输出</strong>"0+-2i"
34+
<strong>解释</strong>(1 - i) * (1 - i) = 1 + i2 - 2 * i = -2i ,你需要将它转换为 0+-2i 的形式。
2735
</pre>
2836

29-
<p><strong>注意:</strong></p>
37+
<p> </p>
3038

31-
<ol>
32-
<li>输入字符串不包含额外的空格。</li>
33-
<li>输入字符串将以&nbsp;<strong>a+bi</strong> 的形式给出,其中整数 <strong>a</strong> 和 <strong>b</strong> 的范围均在 [-100, 100] 之间。<strong>输出也应当符合这种形式</strong>。</li>
34-
</ol>
39+
<p><strong>提示:</strong></p>
40+
41+
<ul>
42+
<li><code>num1</code> 和 <code>num2</code> 都是有效的复数表示。</li>
43+
</ul>
3544

3645
## 解法
3746

3847
<!-- 这里可写通用的实现逻辑 -->
3948

49+
`(a+bi)(c+di) = ac-bd+(ad+cb)i`
50+
4051
<!-- tabs:start -->
4152

4253
### **Python3**
4354

4455
<!-- 这里可写当前语言的特殊实现逻辑 -->
4556

4657
```python
47-
58+
class Solution:
59+
def complexNumberMultiply(self, num1: str, num2: str) -> str:
60+
a, b = map(int, num1[:-1].split('+'))
61+
c, d = map(int, num2[:-1].split('+'))
62+
return f'{a * c - b * d}+{a * d + c * b}i'
4863
```
4964

5065
### **Java**
5166

5267
<!-- 这里可写当前语言的特殊实现逻辑 -->
5368

5469
```java
70+
class Solution {
71+
public String complexNumberMultiply(String num1, String num2) {
72+
String[] c1 = num1.split("\\+|i");
73+
String[] c2 = num2.split("\\+|i");
74+
int a = Integer.parseInt(c1[0]);
75+
int b = Integer.parseInt(c1[1]);
76+
int c = Integer.parseInt(c2[0]);
77+
int d = Integer.parseInt(c2[1]);
78+
return String.format("%d+%di", a * c - b * d, a * d + c * b);
79+
}
80+
}
81+
```
82+
83+
### **C++**
84+
85+
```cpp
86+
class Solution {
87+
public:
88+
string complexNumberMultiply(string num1, string num2) {
89+
int a, b, c, d;
90+
sscanf(num1.c_str(), "%d+%di", &a, &b);
91+
sscanf(num2.c_str(), "%d+%di", &c, &d);
92+
return string(to_string(a * c - b * d) + "+" + to_string(a * d + c * b) + "i");
93+
}
94+
};
95+
```
5596
97+
### **Go**
98+
99+
```go
100+
func complexNumberMultiply(num1, num2 string) string {
101+
parse := func(num string) (a, b int) {
102+
i := strings.IndexByte(num, '+')
103+
a, _ = strconv.Atoi(num[:i])
104+
b, _ = strconv.Atoi(num[i+1 : len(num)-1])
105+
return
106+
}
107+
a, b := parse(num1)
108+
c, d := parse(num2)
109+
return fmt.Sprintf("%d+%di", a*c-b*d, a*d+b*c)
110+
}
56111
```
57112

58113
### **...**

solution/0500-0599/0537.Complex Number Multiplication/README_EN.md

Lines changed: 68 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -4,70 +4,100 @@
44

55
## Description
66

7-
<p>
7+
<p>A <a href="https://en.wikipedia.org/wiki/Complex_number" target="_blank">complex number</a> can be represented as a string on the form <code>&quot;<strong>real</strong>+<strong>imaginary</strong>i&quot;</code> where:</p>
88

9-
Given two strings representing two <a href = "https://en.wikipedia.org/wiki/Complex_number">complex numbers</a>.</p>
9+
<ul>
10+
<li><code>real</code> is the real part and is an integer in the range <code>[-100, 100]</code>.</li>
11+
<li><code>imaginary</code> is the imaginary part and is an integer in the range <code>[-100, 100]</code>.</li>
12+
<li><code>i<sup>2</sup> == -1</code>.</li>
13+
</ul>
1014

11-
<p>
15+
<p>Given two complex numbers <code>num1</code> and <code>num2</code> as strings, return <em>a string of the complex number that represents their multiplications</em>.</p>
1216

13-
You need to return a string representing their multiplication. Note i<sup>2</sup> = -1 according to the definition.
14-
15-
</p>
16-
17-
<p><b>Example 1:</b><br />
17+
<p>&nbsp;</p>
18+
<p><strong>Example 1:</strong></p>
1819

1920
<pre>
20-
21-
<b>Input:</b> "1+1i", "1+1i"
22-
23-
<b>Output:</b> "0+2i"
24-
25-
<b>Explanation:</b> (1 + i) * (1 + i) = 1 + i<sup>2</sup> + 2 * i = 2i, and you need convert it to the form of 0+2i.
26-
21+
<strong>Input:</strong> num1 = &quot;1+1i&quot;, num2 = &quot;1+1i&quot;
22+
<strong>Output:</strong> &quot;0+2i&quot;
23+
<strong>Explanation:</strong> (1 + i) * (1 + i) = 1 + i2 + 2 * i = 2i, and you need convert it to the form of 0+2i.
2724
</pre>
2825

29-
</p>
30-
31-
<p><b>Example 2:</b><br />
26+
<p><strong>Example 2:</strong></p>
3227

3328
<pre>
34-
35-
<b>Input:</b> "1+-1i", "1+-1i"
36-
37-
<b>Output:</b> "0+-2i"
38-
39-
<b>Explanation:</b> (1 - i) * (1 - i) = 1 + i<sup>2</sup> - 2 * i = -2i, and you need convert it to the form of 0+-2i.
40-
29+
<strong>Input:</strong> num1 = &quot;1+-1i&quot;, num2 = &quot;1+-1i&quot;
30+
<strong>Output:</strong> &quot;0+-2i&quot;
31+
<strong>Explanation:</strong> (1 - i) * (1 - i) = 1 + i2 - 2 * i = -2i, and you need convert it to the form of 0+-2i.
4132
</pre>
4233

43-
</p>
34+
<p>&nbsp;</p>
35+
<p><strong>Constraints:</strong></p>
4436

45-
<p><b>Note:</b>
46-
47-
<ol>
48-
49-
<li>The input strings will not have extra blank.</li>
50-
51-
<li>The input strings will be given in the form of <b>a+bi</b>, where the integer <b>a</b> and <b>b</b> will both belong to the range of [-100, 100]. And <b>the output should be also in this form</b>.</li>
52-
53-
</ol>
54-
55-
</p>
37+
<ul>
38+
<li><code>num1</code> and <code>num2</code> are valid complex numbers.</li>
39+
</ul>
5640

5741
## Solutions
5842

43+
`(a+bi)(c+di) = ac-bd+(ad+cb)i`
44+
5945
<!-- tabs:start -->
6046

6147
### **Python3**
6248

6349
```python
64-
50+
class Solution:
51+
def complexNumberMultiply(self, num1: str, num2: str) -> str:
52+
a, b = map(int, num1[:-1].split('+'))
53+
c, d = map(int, num2[:-1].split('+'))
54+
return f'{a * c - b * d}+{a * d + c * b}i'
6555
```
6656

6757
### **Java**
6858

6959
```java
60+
class Solution {
61+
public String complexNumberMultiply(String num1, String num2) {
62+
String[] c1 = num1.split("\\+|i");
63+
String[] c2 = num2.split("\\+|i");
64+
int a = Integer.parseInt(c1[0]);
65+
int b = Integer.parseInt(c1[1]);
66+
int c = Integer.parseInt(c2[0]);
67+
int d = Integer.parseInt(c2[1]);
68+
return String.format("%d+%di", a * c - b * d, a * d + c * b);
69+
}
70+
}
71+
```
72+
73+
### **C++**
74+
75+
```cpp
76+
class Solution {
77+
public:
78+
string complexNumberMultiply(string num1, string num2) {
79+
int a, b, c, d;
80+
sscanf(num1.c_str(), "%d+%di", &a, &b);
81+
sscanf(num2.c_str(), "%d+%di", &c, &d);
82+
return string(to_string(a * c - b * d) + "+" + to_string(a * d + c * b) + "i");
83+
}
84+
};
85+
```
7086
87+
### **Go**
88+
89+
```go
90+
func complexNumberMultiply(num1, num2 string) string {
91+
parse := func(num string) (a, b int) {
92+
i := strings.IndexByte(num, '+')
93+
a, _ = strconv.Atoi(num[:i])
94+
b, _ = strconv.Atoi(num[i+1 : len(num)-1])
95+
return
96+
}
97+
a, b := parse(num1)
98+
c, d := parse(num2)
99+
return fmt.Sprintf("%d+%di", a*c-b*d, a*d+b*c)
100+
}
71101
```
72102

73103
### **...**
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution {
2+
public:
3+
string complexNumberMultiply(string num1, string num2) {
4+
int a, b, c, d;
5+
sscanf(num1.c_str(), "%d+%di", &a, &b);
6+
sscanf(num2.c_str(), "%d+%di", &c, &d);
7+
return string(to_string(a * c - b * d) + "+" + to_string(a * d + c * b) + "i");
8+
}
9+
};
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
func complexNumberMultiply(num1, num2 string) string {
2+
parse := func(num string) (a, b int) {
3+
i := strings.IndexByte(num, '+')
4+
a, _ = strconv.Atoi(num[:i])
5+
b, _ = strconv.Atoi(num[i+1 : len(num)-1])
6+
return
7+
}
8+
a, b := parse(num1)
9+
c, d := parse(num2)
10+
return fmt.Sprintf("%d+%di", a*c-b*d, a*d+b*c)
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution {
2+
public String complexNumberMultiply(String num1, String num2) {
3+
String[] c1 = num1.split("\\+|i");
4+
String[] c2 = num2.split("\\+|i");
5+
int a = Integer.parseInt(c1[0]);
6+
int b = Integer.parseInt(c1[1]);
7+
int c = Integer.parseInt(c2[0]);
8+
int d = Integer.parseInt(c2[1]);
9+
return String.format("%d+%di", a * c - b * d, a * d + c * b);
10+
}
11+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class Solution:
2+
def complexNumberMultiply(self, num1: str, num2: str) -> str:
3+
a, b = map(int, num1[:-1].split('+'))
4+
c, d = map(int, num2[:-1].split('+'))
5+
return f'{a * c - b * d}+{a * d + c * b}i'

0 commit comments

Comments
 (0)