-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBM49.java
More file actions
69 lines (67 loc) · 2.58 KB
/
BM49.java
File metadata and controls
69 lines (67 loc) · 2.58 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
63
64
65
66
67
68
69
package NiukeTOP101;
import java.util.ArrayDeque;
import java.util.Deque;
/**
* 算法:栈
* 1.用栈保存各部分计算的和
* 2.遍历表达式,使用 sign 变量记录运算符,初始化是 '+';使用 number 变量记录字符串中的数字部分的数字值是多少
* 2.0 遇到空格时跳过
* 2.1 遇到数字时继续遍历求这个完整的数字的值,保存到 number 中
* 2.2 遇到左括号时递归求这个括号里面的表达式的值,先遍历找到对应的右括号,因为可能里面还嵌有多对括号,使用一个变量 counterPartition 统计括号对数直到变量为 0
* 2.3 遇到运算符时或者到表达式末尾时,就去计算上一个运算符并把计算结果 push 进栈,然后保存新的运算符到 sign
* 如果是 + ,不要计算,push 进去
* 如果是 - ,push 进去负的当前数
* 如果是 ×、÷ ,pop 出一个运算数和当前数作计算
* 3.最后把栈中的结果求和即可
*/
public class BM49 {
public int solve (String s) {
s = s.trim();
Deque<Integer> stack = new ArrayDeque<>();
int number = 0;
char sign = '+';
char[] charArray = s.toCharArray();
for (int i = 0, n = charArray.length; i < n; i++) {
char c = charArray[i];
if(c == ' '){
continue;
}
if(Character.isDigit(c)){
number = number * 10 + c - '0';
}
if(c == '('){
int j = i + 1;
int counterPartition = 1;
while (counterPartition > 0){
if(charArray[j] == '('){
counterPartition ++;
}
if(charArray[j] == ')'){
counterPartition --;
}
j++;
}
number = solve(s.substring(i+1, j-1));
i = j - 1;
}
if(!Character.isDigit(c) || i == n - 1){
if(sign == '+'){
stack.push(number);
}else if(sign == '-'){
stack.push(-1 * number);
}else if (sign == '*') {
stack.push(stack.pop() * number);
} else if (sign == '/') {
stack.push(stack.pop() / number);
}
number = 0;
sign = c;
}
}
int ans = 0;
while (!stack.isEmpty()) {
ans += stack.pop();
}
return ans;
}
}