Skip to content

Commit af9661e

Browse files
solves basic calcuator ii in java
1 parent 52a3060 commit af9661e

File tree

2 files changed

+89
-1
lines changed

2 files changed

+89
-1
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@
183183
| 223 | [Rectangle Area](https://leetcode.com/problems/rectangle-area) | [![Java](assets/java.png)](src/RectangleArea.java) | |
184184
| 225 | [Implement Stack using Queues](https://leetcode.com/problems/implement-stack-using-queues) | [![Java](assets/java.png)](src/MyStack.java) [![Python](assets/python.png)](python/implement_stack_using_queues.py) | |
185185
| 226 | [Invert Binary Tree](https://leetcode.com/problems/invert-binary-tree) | [![Java](assets/java.png)](src/InvertBinaryTree.java) [![Python](assets/python.png)](python/invert_binary_tree.py) | |
186-
| 228 | [Basic Calculator II](https://leetcode.com/problems/basic-calculator-ii) | | |
186+
| 227 | [Basic Calculator II](https://leetcode.com/problems/basic-calculator-ii) | [![Java](assets/java.png)](src/BasicCalculatorII.java) | |
187187
| 229 | [Majority Element II](https://leetcode.com/problems/majority-element-ii) | | |
188188
| 230 | [Kth Smallest Element in a BST](https://leetcode.com/problems/kth-smallest-element-in-a-bst) | | |
189189
| 231 | [Power of Two](https://leetcode.com/problems/power-of-two) | [![Java](assets/java.png)](src/PowerOf2.java) [![Python](assets/python.png)](python/is_power_of_2.py) | |

src/BasicCalculatorII.java

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
// https://leetcode.com/problems/basic-calculator-ii
2+
// extremely innovative approach, using a grammar to evaluate expressions
3+
// T: O(N)
4+
// S: O(1)
5+
6+
import java.util.Set;
7+
8+
public class BasicCalculatorII {
9+
private static final char ADDITION = '+';
10+
private static final char SUBTRACTION = '-';
11+
private static final char MULTIPLICATION = '*';
12+
private static final char DIVISION = '/';
13+
private static final char SPACE = ' ';
14+
15+
private static final Set<Character> ADDITIVE_OPERATORS = Set.of(ADDITION, SUBTRACTION);
16+
private static final Set<Character> MULTIPLICATIVE_OPERATORS = Set.of(MULTIPLICATION, DIVISION);
17+
18+
private String expression;
19+
private int index;
20+
21+
public int calculate(String s) {
22+
this.expression = s;
23+
this.index = 0;
24+
return addition();
25+
}
26+
27+
private int addition() {
28+
int sum = multiplication();
29+
while (index < expression.length() && isAdditiveOperator(expression.charAt(index))) {
30+
char operator = expression.charAt(index++);
31+
sum = applyOperator(sum, multiplication(), operator);
32+
}
33+
return sum;
34+
}
35+
36+
private int multiplication() {
37+
int result = toNumber();
38+
while (index < expression.length() && isMultiplicativeOperator(expression.charAt(index))) {
39+
char operator = expression.charAt(index++);
40+
result = applyOperator(result, toNumber(), operator);
41+
}
42+
return result;
43+
}
44+
45+
private int toNumber() {
46+
int number = 0;
47+
while (index < expression.length() && isDigitOrSpace(expression.charAt(index))) {
48+
if (isSpace(expression.charAt(index))) {
49+
index++;
50+
continue;
51+
}
52+
number *= 10;
53+
number += toDigit(expression.charAt(index));
54+
index++;
55+
}
56+
return number;
57+
}
58+
59+
private int toDigit(char digit) {
60+
return digit - '0';
61+
}
62+
63+
private boolean isDigitOrSpace(char c) {
64+
return Character.isDigit(c) || isSpace(c);
65+
}
66+
67+
private boolean isSpace(char c) {
68+
return c == SPACE;
69+
}
70+
71+
private int applyOperator(int a, int b, char operator) {
72+
return switch (operator) {
73+
case ADDITION -> a + b;
74+
case SUBTRACTION -> a - b;
75+
case MULTIPLICATION -> a * b;
76+
case DIVISION -> a / b;
77+
default -> throw new IllegalStateException("Unexpected value: " + operator);
78+
};
79+
}
80+
81+
private boolean isAdditiveOperator(char operator) {
82+
return ADDITIVE_OPERATORS.contains(operator);
83+
}
84+
85+
private boolean isMultiplicativeOperator(char operator) {
86+
return MULTIPLICATIVE_OPERATORS.contains(operator);
87+
}
88+
}

0 commit comments

Comments
 (0)