|
|
|
|
@ -2,7 +2,18 @@ package class118;
|
|
|
|
|
|
|
|
|
|
import java.util.LinkedList;
|
|
|
|
|
|
|
|
|
|
// 本题测试链接 : https://leetcode.com/problems/basic-calculator-iii/
|
|
|
|
|
// 给定一个字符串表达式str,str表示一个公式,
|
|
|
|
|
// 公式里可能有整数、加减乘除符号和左右括号
|
|
|
|
|
// 返回公式的计算结果
|
|
|
|
|
// 难点在于括号可能嵌套很多层,
|
|
|
|
|
// str="48*((70-65)-43)+8*1",返回-1816
|
|
|
|
|
// str="3+1*4",返回7。str="3+(1*4)",返回7。
|
|
|
|
|
// 1,可以认为给定的字符串一定是正确的公式,即不需要对str做公式有效性检查
|
|
|
|
|
// 2,如果是负数,就需要用括号括起来,
|
|
|
|
|
// 比如"4*(-3)"但如果负数作为公式的开头或括号部分的开头则可以没有括号,
|
|
|
|
|
// 比如"-3*4"和"(-3*4)"都是合法的
|
|
|
|
|
// 3,不用考虑计算过程中会发生溢出的情况。
|
|
|
|
|
// 测试链接 : https://leetcode.cn/problems/basic-calculator-iii/
|
|
|
|
|
public class Code01_ExpressionCompute {
|
|
|
|
|
|
|
|
|
|
public static int calculate(String str) {
|
|
|
|
|
|