题目描述
blablabla
样例
blablabla
算法1
(暴力枚举) $O(n^2)$
blablabla
时间复杂度
参考文献
java 代码
blablabla
import java.util.*;
class Main{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Solution solution = new Solution();
String s = sc.nextLine();
System.out.print(solution.calculate(s));
}
}
class Solution{
int i = 0;
public int calculate(String s) {
int num = 0;
char operator = ‘+’;
Deque[HTML_REMOVED] stack = new ArrayDeque<>();
while (i < s.length()) {
char c = s.charAt(i++);
if (Character.isDigit(c)) {
num = num * 10 + c - ‘0’;
} else if (c == ‘(‘) {
num = calculate(s);
}
if (i == s.length() || c == ‘+’ || c == ‘-‘ || c == ‘/’ || c == ‘‘ || c == ‘)’) { // 记得 c == ‘)’ 也要判定把数据压入stack
if (operator == ‘+’) {
stack.push(num);
} else if (operator == ‘-‘) {
stack.push(-num);
} else if (operator == ‘/’) {
stack.push(stack.pop() / num);
} else if (operator == ‘’) {
stack.push(stack.pop() * num);
}
operator = c;
num = 0;
}
if (c == ‘)’) break;
}
int res = 0;
for (int key : stack) res += key;
return res;
}
}
算法2
(暴力枚举) $O(n^2)$
blablabla
时间复杂度
参考文献
C++ 代码
blablabla