-
Notifications
You must be signed in to change notification settings - Fork 2
/
BasicCalculatorII.java
49 lines (45 loc) · 1.54 KB
/
BasicCalculatorII.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
42
43
44
45
46
47
48
49
package src.java.medium.string;
import java.util.LinkedList;
public class BasicCalculatorII {
public int calculate(String s) {
if (s==null || s.trim().isEmpty()) return 0;
LinkedList<Integer> nums = new LinkedList<>();
LinkedList<Character> operators = new LinkedList<>();
int i = 0;
while (i < s.length()) {
char c = s.charAt(i);
if (c == ' ') i++;
else if (Character.isDigit(c)) {
int t = i;
while (i<s.length() && Character.isDigit(s.charAt(i)))
i++;
int n = Integer.parseInt(s.substring(t, i));
nums.offer(n);
} else {
operators.offer(c);
i++;
}
}
LinkedList<Integer> nums2 = new LinkedList<>();
LinkedList<Character> operators2 = new LinkedList<>();
nums2.offer(nums.poll());
while(!operators.isEmpty()) {
int n1 = nums.poll();
char op = operators.poll();
if (op == '*') nums2.offer(n1*nums2.pollLast());
else if (op == '/') nums2.offer(nums2.pollLast()/n1);
else {
nums2.offer(n1);
operators2.offer(op);
}
}
int ret = nums2.poll();
while (!operators2.isEmpty()) {
int n1 = nums2.poll();
char op = operators2.poll();
if (op == '+') ret += n1;
else if (op == '-') ret -= n1;
}
return ret;
}
}