-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathBasicCalculatorIII.java
More file actions
175 lines (162 loc) · 5.69 KB
/
BasicCalculatorIII.java
File metadata and controls
175 lines (162 loc) · 5.69 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package stack;
// Source : https://leetcode.com/problems/basic-calculator-iii/
// Id : 772
// Author : Fanlu Hai | https://github.com/Fanlu91/FanluLeetcode
// Date : 2022/2/8
// Topic : stack
// Level : Hard
// Other :
// Tips :
// Links :
// Result : 46.17% 20.92%
import java.util.*;
public class BasicCalculatorIII {
Map<Character, Integer> map = new HashMap<Character, Integer>() {{
put('-', 1);
put('+', 1);
put('*', 2);
put('/', 2);
put('%', 2);
put('^', 3);
}};
// 5ms
public int calculate(String s) {
char[] arr = s.toCharArray();
Deque<Integer> nums = new ArrayDeque<>();
nums.addLast(0); // 为了防止第一个数为负数,先往 nums 加个 0
Deque<Character> ops = new ArrayDeque<>();
for (int i = 0; i < s.length(); i++) {
char c = arr[i];
if (c == ' ')
continue;
if (c == '(') {
ops.addLast(c);
} else if (c == ')') {
// 计算到最近一个左括号为止
while (!ops.isEmpty()) {
if (ops.peekLast() != '(') {
calc(nums, ops);
} else {
ops.pollLast();
break;
}
}
} else if (Character.isDigit(c)) {
int val = c - '0';
while (i + 1 < s.length() && Character.isDigit(arr[i + 1])) {
i++;
val = val * 10 + (arr[i] - '0');
}
nums.addLast(val);
} else {
if (i > 0 && (arr[i - 1] == '(')) { // (+ (- 情况处理
nums.addLast(0);
}
// 有一个新操作要入栈时,先把栈内可以算的都算了
// 只有满足「栈内运算符」比「当前运算符」优先级高/同等,才进行运算
while (!ops.isEmpty() && ops.peekLast() != '(') {
char preOps = ops.peekLast();
if (map.get(preOps) >= map.get(c)) {
calc(nums, ops);
} else {
break;
}
}
ops.addLast(c);
}
}
// 将剩余的计算完
while (!ops.isEmpty())
calc(nums, ops);
return nums.peekLast();
}
private void calc(Deque<Integer> nums, Deque<Character> ops) {
if (nums.isEmpty() || nums.size() < 2)
return;
if (ops.isEmpty())
return;
int b = nums.pollLast(), a = nums.pollLast();
char op = ops.pollLast();
int ans = 0;
if (op == '+') ans = a + b;
else if (op == '-') ans = a - b;
else if (op == '*') ans = a * b;
else if (op == '/') ans = a / b;
else if (op == '^') ans = (int) Math.pow(a, b);
else if (op == '%') ans = a % b;
nums.addLast(ans);
}
// 7ms
// using stack instead of deque
public int calculate1(String s) {
// public int calculate(String s) {
// 将所有的空格去掉
char[] arr = s.toCharArray();
Stack<Integer> nums = new Stack<>();
// 为了防止第一个数为负数,先往 nums 加个 0
nums.push(0);
Stack<Character> ops = new Stack<>();
for (int i = 0; i < s.length(); i++) {
char c = arr[i];
if (c == ' ')
continue;
if (c == '(') {
ops.push(c);
} else if (c == ')') {
// 计算到最近一个左括号为止
while (!ops.isEmpty()) {
if (ops.peek() != '(') {
calc1(nums, ops);
} else {
ops.pop();
break;
}
}
} else {
if (Character.isDigit(c)) {
int val = c - '0';
while (i + 1 < s.length() && Character.isDigit(arr[i + 1])) {
i++;
val = val * 10 + (arr[i] - '0');
}
nums.push(val);
} else {
if (i > 0 && (arr[i - 1] == '(' || arr[i - 1] == '+' || arr[i - 1] == '-')) {
nums.push(0);
}
// 有一个新操作要入栈时,先把栈内可以算的都算了
// 只有满足「栈内运算符」比「当前运算符」优先级高/同等,才进行运算
while (!ops.isEmpty() && ops.peek() != '(') {
char prev = ops.peek();
if (map.get(prev) >= map.get(c)) {
calc1(nums, ops);
} else {
break;
}
}
ops.push(c);
}
}
}
// 将剩余的计算完
while (!ops.isEmpty())
calc1(nums, ops);
return nums.pop();
}
void calc1(Stack<Integer> nums, Stack<Character> ops) {
if (nums.isEmpty() || nums.size() < 2)
return;
if (ops.isEmpty())
return;
int b = nums.pop(), a = nums.pop();
char op = ops.pop();
int ans = 0;
if (op == '+') ans = a + b;
else if (op == '-') ans = a - b;
else if (op == '*') ans = a * b;
else if (op == '/') ans = a / b;
else if (op == '^') ans = (int) Math.pow(a, b);
else if (op == '%') ans = a % b;
nums.push(ans);
}
}