Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit6bc451c

Browse files
[LEET-537] add 537
1 parenta060f63 commit6bc451c

File tree

3 files changed

+101
-0
lines changed

3 files changed

+101
-0
lines changed

‎leetcode-algorithms/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
|540|[Single Element in a Sorted Array](https://leetcode.com/problems/single-element-in-a-sorted-array/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/SingleElementinaSortedArray.java)| O(n)|O(1)| Medium|
1212
|539|[Minimum Time Difference](https://leetcode.com/problems/minimum-time-difference/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/MinimumTimeDifference.java) | O(n) |O(1) | Medium | String
1313
|538|[Convert BST to Greater Tree](https://leetcode.com/problems/convert-bst-to-greater-tree/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/ConvertBSTtoGreaterTree.java) | O(nlogn) |O(n) | Medium | Tree
14+
|537|[Complex Number Multiplication](https://leetcode.com/problems/complex-number-multiplication/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/ComplexNumberMultiplication.java) | O(1) |O(1) | Medium | Math, String
1415
|536|[Construct Binary Tree from String](https://leetcode.com/problems/construct-binary-tree-from-string/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/ConstructBinaryTreefromString.java) | O(n) |O(h) | Medium | Recursion
1516
|535|[Encode and Decode TinyURL](https://leetcode.com/problems/encode-and-decode-tinyurl/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/EncodeandDecodeTinyURL.java) | O(1) |O(n) | Medium | Design
1617
|532|[K-diff Pairs in an Array](https://leetcode.com/problems/k-diff-pairs-in-an-array/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/KdiffPairsinanArray.java) | O(n) |O(n) | Easy | HashMap
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
packagecom.stevesun.solutions;
2+
3+
/**
4+
* Given two strings representing two complex numbers.
5+
6+
You need to return a string representing their multiplication. Note i2 = -1 according to the definition.
7+
8+
Example 1:
9+
Input: "1+1i", "1+1i"
10+
Output: "0+2i"
11+
Explanation: (1 + i) * (1 + i) = 1 + i2 + 2 * i = 2i, and you need convert it to the form of 0+2i.
12+
13+
Example 2:
14+
Input: "1+-1i", "1+-1i"
15+
Output: "0+-2i"
16+
Explanation: (1 - i) * (1 - i) = 1 + i2 - 2 * i = -2i, and you need convert it to the form of 0+-2i.
17+
18+
Note:
19+
The input strings will not have extra blank.
20+
The input strings will be given in the form of a+bi, where the integer a and b will both belong to the range of [-100, 100]. And the output should be also in this form.
21+
*/
22+
publicclassComplexNumberMultiplication {
23+
24+
publicStringcomplexNumberMultiply(Stringa,Stringb) {
25+
String[]part1And2 =a.split("\\+");
26+
String[]part3And4 =b.split("\\+");
27+
Stringproduct1 =String.valueOf(Integer.parseInt(part1And2[0]) *Integer.parseInt(part3And4[0]));//this is real number multiplication
28+
Stringproduct2 =multiply(part1And2[0],part3And4[1]);
29+
Stringproduct3 =multiply(part3And4[0],part1And2[1]);
30+
Stringproduct4 =multiplyTwoIs(part3And4[1],part1And2[1]);
31+
StringtwoISum =sumTwoI(product2,product3);
32+
StringnumberValue =String.valueOf(Integer.valueOf(product1) +Integer.valueOf(product4));
33+
returnnumberValue +"+" +twoISum;
34+
}
35+
36+
privateStringsumTwoI(Stringproduct2,Stringproduct3) {
37+
intnumber2 =Integer.parseInt(product2.substring(0,product2.length()-1));
38+
intnumber3 =Integer.parseInt(product3.substring(0,product3.length()-1));
39+
returnString.valueOf(number2 +number3) +"i";
40+
}
41+
42+
privateStringmultiplyTwoIs(Stringp,Stringq) {
43+
intnumber1 =Integer.parseInt(p.substring(0,p.length()-1));
44+
intnumber2 =Integer.parseInt(q.substring(0,q.length()-1));
45+
intnumberProduct =number1 *number2;
46+
returnString.valueOf(-numberProduct);
47+
}
48+
49+
privateStringmultiply(Stringp,StringwithI) {
50+
intnumberPart =Integer.parseInt(withI.substring(0,withI.length()-1));
51+
returnString.valueOf(numberPart *Integer.valueOf(p)) +"i";
52+
}
53+
54+
55+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
packagecom.stevesun;
2+
3+
importcom.stevesun.solutions.ComplexNumberMultiplication;
4+
importorg.junit.Before;
5+
importorg.junit.BeforeClass;
6+
importorg.junit.Test;
7+
8+
importstaticjunit.framework.Assert.assertEquals;
9+
10+
/**
11+
* Created by stevesun on 1/25/17.
12+
*/
13+
publicclassComplexNumberMultiplicationTest {
14+
privatestaticComplexNumberMultiplicationtest;
15+
privatestaticStringexpected;
16+
privatestaticStringactual;
17+
privatestaticStringa;
18+
privatestaticStringb;
19+
20+
@BeforeClass
21+
publicstaticvoidsetup(){
22+
test =newComplexNumberMultiplication();
23+
}
24+
25+
@Before
26+
publicvoidsetupForEachTest(){}
27+
28+
@Test
29+
publicvoidtest1(){
30+
expected ="0+2i";
31+
a ="1+1i";
32+
b ="1+1i";
33+
actual =test.complexNumberMultiply(a,b);
34+
assertEquals(expected,actual);
35+
}
36+
37+
@Test
38+
publicvoidtest2(){
39+
expected ="0+-2i";
40+
a ="1+-1i";
41+
b ="1+-1i";
42+
actual =test.complexNumberMultiply(a,b);
43+
assertEquals(expected,actual);
44+
}
45+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp