-
Notifications
You must be signed in to change notification settings - Fork 7
/
Main.java
41 lines (37 loc) · 1.34 KB
/
Main.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package com.company;
import java.util.Stack;
public class Main {
public static void main(String[] args) {
String s = "(1+(4+5+2)-3)+(6+8)";
System.out.println(calculate(s));
}
public static int calculate(String s) {
StringBuilder num = new StringBuilder();
Stack<Character> operators = new Stack<>();
Stack<Integer> nums = new Stack<>();
for (Character l : s.toCharArray()){
if(l == ' ') continue;
if(Character.isDigit(l))
num.append(l);
else {
if(num.length() != 0) nums.push(Integer.parseInt(num.toString()));
num = new StringBuilder();
if(l == ')'){
int temp = 0;
while (!operators.isEmpty() && operators.peek() != '(')
temp += operators.pop() == '-' ? -nums.pop() : nums.pop();
temp += nums.pop();
operators.pop();
nums.push(temp);
}
else
operators.push(l);
}
}
if(num.length() != 0) nums.push(Integer.parseInt(num.toString()));
int res = 0;
while (!nums.isEmpty())
res += operators.isEmpty() || operators.pop() == '+' ? nums.pop() : -nums.pop();
return res;
}
}