Skip to content

Commit 61b9c35

Browse files
authored
Update Basic Calculator.java
1 parent 2bad6aa commit 61b9c35

File tree

1 file changed

+14
-31
lines changed

1 file changed

+14
-31
lines changed

Java/Basic Calculator.java

Lines changed: 14 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -58,34 +58,29 @@ public TreeNode(int weight, String str) {
5858
}
5959

6060
public int calculate(String s) {
61-
// check edge case
62-
if (s == null || s.length() == 0) {
63-
return 0;
64-
}
65-
// build expression tree
66-
TreeNode root = buildTree(s);
67-
// post-order traversal of the tree
68-
69-
return (int)evaluate(root);
61+
if (s == null || s.length() == 0) return 0;
62+
TreeNode root = buildTree(s); // build expression tree
63+
return (int)evaluate(root); // post-order traversal of the tree
7064
}
7165

7266
// build tree based on input string, min-tree. return root
7367
private TreeNode buildTree(String s) {
68+
int n = s.length();
7469
char[] chars = s.trim().toCharArray();
7570
Stack<TreeNode> stack = new Stack<>();
7671
int base = 0;
7772
StringBuffer sb = new StringBuffer();
78-
for (int i = 0; i < chars.length; i++) {
73+
for (int i = 0; i < n; i++) {
7974
char c = chars[i];
8075
if (c == ' ') {
8176
continue;
82-
} else if (c == '(') {
77+
} else if (c == '(') { // '()' are used to add weight, not stored in tree
8378
base = getWeight(base, c);
8479
continue;
8580
} else if (c == ')') {
8681
base = getWeight(base, c);
8782
continue;
88-
} else if (i < chars.length - 1 && isDigit(chars[i]) && isDigit(chars[i + 1])) {
83+
} else if (i < n - 1 && isDigit(chars[i]) && isDigit(chars[i + 1])) { // continue to get remaining of the int
8984
sb.append(c);
9085
continue;
9186
}
@@ -113,14 +108,12 @@ private TreeNode buildTree(String s) {
113108
root = stack.pop();
114109
}
115110

116-
return root; // it's the root of tree
111+
return root; // it's the root of tree, always a operator
117112
}
118113

119114
// post-order traversal to evaluate the expression
120115
private long evaluate(TreeNode root) {
121-
if (root == null) {
122-
return 0;
123-
}
116+
if (root == null) return 0;
124117
long left = evaluate(root.left);
125118
long right = evaluate(root.right);
126119
long result = 0;
@@ -146,26 +139,16 @@ private long evaluate(TreeNode root) {
146139
// get weight of the character. integer weights the most and will be leaf.
147140
// Remember to store the result using long
148141
private int getWeight(int base, char c) {
149-
if (c == '(') {
150-
return base + 10;
151-
}
152-
if (c == ')') {
153-
return base - 10;
154-
}
155-
if (c == '+' || c == '-') {
156-
return base + 1;
157-
}
158-
if (c == '*' || c == '/') {
159-
return base + 2;
160-
}
142+
if (c == '(') return base + 10;
143+
if (c == ')') return base - 10;
144+
if (c == '+' || c == '-') return base + 1;
145+
if (c == '*' || c == '/') return base + 2;
161146
return Integer.MAX_VALUE;
162147
}
163148

164149
private boolean isDigit(char c) {
165150
return c >= '0' && c <= '9';
166151
}
167-
168-
169152
}
170153

171-
```
154+
```

0 commit comments

Comments
 (0)