# LeetCode 224 Basic Calculator (后缀表达式 栈)

888人阅读 评论(0)

Implement a basic calculator to evaluate a simple expression string.

The expression string may contain open ( and closing parentheses ), the plus + or minus sign -, non-negative integers and empty spaces .

You may assume that the given expression is always valid.

Some examples:

"1 + 1" = 2
" 2-1 + 2 " = 3
"(1+(4+5+2)-3)+(6+8)" = 23


Note: Do not use the eval built-in library function.

1.若为数字直接加到后面

2.若为'('，入符号栈

3.若为运算符，则将优先级大于等于它的运算符先弹出并记录带答案，再将其入栈，本题运算符只有+，-，优先级相同

4.若为')'，弹出运算符直到遇到‘(’

public class Solution {

public void solve(Stack<Integer> stkNum, char op) {
int a = stkNum.peek();
stkNum.pop();
int b = stkNum.peek();
stkNum.pop();
if(op == '+') {
stkNum.push(b + a);
}
else if(op == '-') {
stkNum.push(b - a);
}
}

public String infixToSuffix(String s) {
StringBuffer sb = new StringBuffer("");
Stack<Character> stkOp = new Stack<>();
int num, len = s.length();
for(int i = 0; i < len; i ++) {
char ch = s.charAt(i);
if(ch == ' ') {
continue;
}
else if(ch == '(') {
stkOp.push(ch);
}
else if(ch == '+' || ch == '-') {
while(stkOp.size() > 0 && stkOp.peek() != '(') {
sb.append(stkOp.peek());
stkOp.pop();
}
stkOp.push(ch);
}
else if(ch == ')') {
while(stkOp.peek() != '(') {
sb.append(stkOp.peek());
stkOp.pop();
}
stkOp.pop();
}
else {
num = 0;
while(i < len && s.charAt(i) >= '0' && s.charAt(i) <= '9') {
sb.append(s.charAt(i));
i ++;
}
sb.append(' ');
i --;
}
}
return sb.toString();
}

public int calculate(String s) {
Stack<Integer> stkNum = new Stack<>();
String str = infixToSuffix('(' + s + ')');
int num = 0;
//System.out.println("str = " + str);
for(int i = 0; i < str.length(); i ++) {
if(str.charAt(i) == '+') {
solve(stkNum, '+');
}
else if(str.charAt(i) == '-') {
solve(stkNum, '-');
}
else {
num = 0;
while(str.charAt(i) != ' ') {
num = num * 10 + str.charAt(i) - '0';
i ++;
}
stkNum.push(num);
}
}
return stkNum.peek();
}
}

public class Solution {
public static int calculate(String s) {
if (s.length() == 0) {
return 0;
}
s = "(" + s + ")";
int[] pos = {0};
return eval(s, pos);
}

private static int eval(String s, int[] pos) {
int val = 0, i = pos[0], sign = 1, num = 0;
while(i < s.length()) {
char c = s.charAt(i);
switch(c) {
case '+': val = val + sign * num; num = 0; sign = 1; i++; break;
case '-': val = val + sign * num; num = 0; sign = -1; i++; break;
case '(': pos[0] = i + 1; val = val + sign * eval(s, pos); i = pos[0]; break;
case ')': pos[0] = i + 1; return val + sign * num;
case ' ': i++; continue;
default : num = num * 10 + c - '0'; i++;
}
}
return val;
}
}

0
0

个人资料
• 访问：746804次
• 积分：15367
• 等级：
• 排名：第874名
• 原创：818篇
• 转载：7篇
• 译文：0篇
• 评论：123条
博客专栏
 LeetCode 文章：135篇 阅读：78746
 Java学习笔记 文章：10篇 阅读：11343
 算法题 文章：50篇 阅读：34307
阅读排行