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

Commit0b369d3

Browse files
add 1628
1 parenta779a8b commit0b369d3

File tree

3 files changed

+139
-0
lines changed

3 files changed

+139
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ _If you like this project, please leave me a star._ ★
182182
|1636|[Sort Array by Increasing Frequency](https://leetcode.com/problems/sort-array-by-increasing-frequency/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1636.java)||Easy|Array, Sort|
183183
|1630|[Arithmetic Subarrays](https://leetcode.com/problems/arithmetic-subarrays/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1630.java)||Medium|Sort|
184184
|1629|[Slowest Key](https://leetcode.com/problems/slowest-key/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1629.java)||Easy|Array|
185+
|1628|[Design an Expression Tree With Evaluate Function](https://leetcode.com/problems/design-an-expression-tree-with-evaluate-function/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1628.java)||Medium|Stack, Binary Tree, Design, Math|
185186
|1626|[Best Team With No Conflicts](https://leetcode.com/problems/best-team-with-no-conflicts/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1626.java)||Medium|DP|
186187
|1625|[Lexicographically Smallest String After Applying Operations](https://leetcode.com/problems/lexicographically-smallest-string-after-applying-operations/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1625.java)||Medium|BFS, DFS|
187188
|1624|[Largest Substring Between Two Equal Characters](https://leetcode.com/problems/largest-substring-between-two-equal-characters/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1624.java)|[:tv:](https://youtu.be/rfjeFs3JuYM)|Easy|String|
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
packagecom.fishercoder.solutions;
2+
3+
importjava.util.Deque;
4+
importjava.util.LinkedList;
5+
importjava.util.List;
6+
7+
publicclass_1628 {
8+
publicstaticclassSolution1 {
9+
/**
10+
* Being able to think of resorting to Stack data structure is the key here to a straightforward solution.
11+
*/
12+
13+
publicabstractstaticclassNode {
14+
publicabstractintevaluate();
15+
16+
publicabstractList<String>print(Nodenode,List<String>list);
17+
18+
// define your fields here
19+
Nodeleft;
20+
Noderight;
21+
Stringval;
22+
}
23+
24+
staticclassNodeImplextendsNode {
25+
26+
@Override
27+
publicintevaluate() {
28+
returninorder(this);
29+
}
30+
31+
privateintinorder(Nodenode) {
32+
if (node ==null) {
33+
return0;
34+
}
35+
if (node.left ==null &&node.right ==null) {
36+
returnInteger.parseInt(node.val);
37+
}
38+
Stringop =node.val;
39+
intleft =inorder(node.left);
40+
intright =inorder(node.right);
41+
if (op.equals("+")) {
42+
returnleft +right;
43+
}elseif (op.equals("-")) {
44+
returnleft -right;
45+
}elseif (op.equals("*")) {
46+
returnleft *right;
47+
}else {
48+
returnleft /right;
49+
}
50+
}
51+
52+
publicNodeImpl(Stringval) {
53+
this.val =val;
54+
}
55+
56+
@Override
57+
publicList<String>print(Nodenode,List<String>list) {
58+
if (node ==null) {
59+
returnlist;
60+
}
61+
print(node.left,list);
62+
list.add(node.val);
63+
print(node.right,list);
64+
returnlist;
65+
}
66+
67+
}
68+
69+
publicstaticclassTreeBuilder {
70+
publicNodebuildTree(String[]postfix) {
71+
Deque<Node>stack =newLinkedList<>();
72+
for (Stringstr :postfix) {
73+
Nodenode =newNodeImpl(str);
74+
if (!Character.isDigit(str.charAt(0))) {
75+
node.right =stack.pollLast();
76+
node.left =stack.pollLast();
77+
}
78+
stack.addLast(node);
79+
}
80+
returnstack.pop();
81+
}
82+
83+
}
84+
}
85+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
packagecom.fishercoder;
2+
3+
importcom.fishercoder.common.utils.CommonUtils;
4+
importcom.fishercoder.solutions._1628;
5+
importorg.junit.BeforeClass;
6+
importorg.junit.Test;
7+
8+
importjava.util.ArrayList;
9+
importjava.util.List;
10+
11+
importstaticorg.junit.Assert.assertEquals;
12+
13+
publicclass_1628Test {
14+
privatestatic_1628.Solution1.TreeBuildertreeBuilderSolution1;
15+
16+
@BeforeClass
17+
publicstaticvoidsetup() {
18+
treeBuilderSolution1 =new_1628.Solution1.TreeBuilder();
19+
}
20+
21+
@Test
22+
publicvoidtest1() {
23+
_1628.Solution1.Nodenode =treeBuilderSolution1.buildTree(newString[]{"3","4","+"});
24+
List<String>list =node.print(node,newArrayList<>());
25+
CommonUtils.printList(list);
26+
assertEquals(7,node.evaluate());
27+
}
28+
29+
@Test
30+
publicvoidtest2() {
31+
_1628.Solution1.Nodenode =treeBuilderSolution1.buildTree(newString[]{"3","4","+","2","*","7","/"});
32+
List<String>list =node.print(node,newArrayList<>());
33+
CommonUtils.printList(list);
34+
assertEquals(2,node.evaluate());
35+
}
36+
37+
@Test
38+
publicvoidtest3() {
39+
_1628.Solution1.Nodenode =treeBuilderSolution1.buildTree(newString[]{"4","5","2","7","+","-","*"});
40+
List<String>list =node.print(node,newArrayList<>());
41+
CommonUtils.printList(list);
42+
assertEquals(-16,node.evaluate());
43+
}
44+
45+
@Test
46+
publicvoidtest4() {
47+
_1628.Solution1.Nodenode =treeBuilderSolution1.buildTree(newString[]{"4","2","+","3","5","1","-","*","+"});
48+
List<String>list =node.print(node,newArrayList<>());
49+
CommonUtils.printList(list);
50+
assertEquals(18,node.evaluate());
51+
}
52+
53+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp