Repository navigation
Expand file tree
/
Copy pathLc227.java
More file actions
62 lines (59 loc) · 1.84 KB
/
Copy pathLc227.java
File metadata and controls
62 lines (59 loc) · 1.84 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
package leetcode;
import java.util.HashMap;
import java.util.Stack;
/**
* @author Kuma
* @date 2021年3月11日
* 227. 基本计算器 II
*/
public class Lc227 {
public static int calculate(String s) {
// 数值和算符栈
Stack<Integer> numSt = new Stack<>();
Stack<Character> opTt = new Stack<>();
HashMap<Character,Integer> map = new HashMap<>();
map.put('+', 1);
map.put('-', 1);
map.put('*', 2);
map.put('/', 2);
s = s.replaceAll(" ","");
char[] ar = s.toCharArray();
int i = 0;
while (i < ar.length){
int num = 0;
while (i < ar.length && Character.isDigit(ar[i])){
num = num * 10 + ar[i++] - '0';
}
numSt.push(num);
if (i < ar.length && ar[i] != ' '){
while (!opTt.isEmpty() && map.get(ar[i]) <= map.get(opTt.peek())) {
int a = numSt.pop();
int b = numSt.pop();
int flag = map.get(opTt.peek());
if (flag == 1) {
numSt.push(opTt.pop() == '+' ? b + a : b - a);
} else {
numSt.push(opTt.pop() == '*' ? b * a : b / a);
}
}
opTt.push(ar[i]);
}
i++;
}
while (!opTt.isEmpty()) {
int a = numSt.pop();
int b = numSt.pop();
int flag = map.get(opTt.peek());
if (flag == 1) {
numSt.push(opTt.pop() == '+' ? b + a : b - a);
} else {
numSt.push(opTt.pop() == '*' ? b * a : b / a);
}
}
return numSt.pop();
}
public static void main(String[] args) {
String s = " 3+5 / 2";
System.out.println(calculate(s));
}
}