forked from lazzzis/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
44 lines (44 loc) · 1.45 KB
/
Solution.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
class Solution {
public int calculate(String s) {
s = s.replace(" ", "");
ArrayList<String> list = new ArrayList<>();
int i = 0;
while (i < s.length()) {
if (!Character.isDigit(s.charAt(i))) {
list.add(String.valueOf(s.charAt(i)));
} else {
int j = i;
while (j < s.length() && Character.isDigit(s.charAt(j))) { j++; }
list.add(s.substring(i, j));
i = j - 1;
}
i++;
}
i = 0;
// list.forEach(System.out::println);
LinkedList<String> l2 = new LinkedList<>();
while (i < list.size()) {
String item = list.get(i);
if (item.equals("*") || item.equals("/")) {
int temp = Integer.parseInt(l2.remove(l2.size() - 1));
int next = Integer.parseInt(list.get(i + 1));
if (item.equals("*")) temp *= next;
else temp /= next;
l2.add(temp + "");
i++;
} else {
l2.add(item);
}
i++;
}
// l2.forEach(System.out::println);
int temp = Integer.parseInt(l2.remove(0));
while (l2.size() > 0) {
String op = l2.remove(0);
int next = Integer.parseInt(l2.remove(0));
if (op.equals("+")) temp += next;
else temp -= next;
}
return temp;
}
}