【百日算法计划】:每日一题,见证成长(026)

题目

给定一个包含正整数、加(+)、减(-)、乘(*)、除(/)的算数表达式(括号除外),计算其结果。
表达式仅包含非负整数,+, - , ,/ 四种运算符和空格 。 整数除法仅保留整数部分。 * *

示例 1:

输入: "3+2X2"

输出: 7

cpp 复制代码
import java.util.Stack;

public class Code {
    public int calculate(String s) {
        Stack<Integer> data_stack = new Stack<>();
        Stack<Character> char_stack = new Stack<>();

        int length = s.length();
        int i = 0;
        while (i < length){
            char c = s.charAt(i);
            if (c == ' '){ //空格直接跳过
                i++;
            } else if (checkNumber(c)){ //如果是数字,处理如多个数字的情况 如333+1中的333
                int tmp = 0;
                while (i < length && checkNumber(s.charAt(i))){
                    tmp = tmp * 10 + (s.charAt(i) - '0');
                    i++;
                }
                data_stack.push(tmp);
            } else if (c == '('){ //左括号 直接入栈
                char_stack.push(c);
                i++;
            } else if (c == ')'){ //右括号 出栈计算 直到碰到'('为止
                while (!char_stack.isEmpty() && char_stack.peek() != '('){
                    fetchAndCol(data_stack,char_stack);
                }
                char_stack.pop(); //弹出'('
                i++;
            } else {
                //如果字符栈为空 或者 运算字符 优先级 大于 栈顶字符,则运算字符直接入栈
                if (char_stack.isEmpty() || checkChar(c,char_stack.peek())){
                    char_stack.push(c);
                } else {
                    //优先级: 运算字符 <= 栈顶字符 ,则拿数字栈的前两个数字和栈顶字符栈计算,并把结果压入数字栈
                    while (!char_stack.isEmpty() && !checkChar(c,char_stack.peek())){
                        fetchAndCol(data_stack,char_stack);
                    }
                    char_stack.push(c); //最后再把字符入字符栈
                }
                i++;
            }
        }
        //如果字符栈不为空,则开始最后的计算,最后数字栈里只有一个最后计算的结果
        while (!char_stack.isEmpty()){
            fetchAndCol(data_stack,char_stack);
        }
        return data_stack.pop();
    }

    //计算数字栈和运算符栈
    public void fetchAndCol(Stack<Integer> data_stack,Stack<Character> char_stack){
        Integer pop1 = data_stack.pop();
        Integer pop2 = data_stack.pop();
        Character pop = char_stack.pop();
        Integer math = math(pop1, pop2, pop);
        data_stack.push(math);
    }

    public Integer math(Integer number1,Integer number2,char c){
        if (c == '+') return number1 + number2;
        if (c == '-') return number2 - number1;
        if (c == '*') return number1 * number2;
        if (c == '/') return number2 / number1;
        else return -1;
    }

    public boolean checkChar(char c,char top){
        if ((c == '*' || c == '/') && (top == '+' || top == '-')) return true;
        if (top == '(') return true; //栈顶运算符是'(' 也是返回true
        else return false;
    }

    //判断是否是数字
    public boolean checkNumber(char c){
        if (c >= '0' && c <= '9') return true;
        else return false;
    }
}
相关推荐
HPtiger10 小时前
数据结构——选择排序
数据结构·算法·排序算法
康实训10 小时前
老年评估仿真实训平台架构设计与老年人能力评估实训室教学落地实践
大数据·人工智能·算法·实训室·实训室建设
DongQiShanRen10 小时前
六根硬件映射(四):归属澄清、两处不一致与卷十一总表
开发语言·人工智能·算法·rust·哈希算法·集成学习
CCblandy10 小时前
雪花 ID 四个边界实测:时钟回拨重号 500 个,前端 JSON.parse 直接丢精度
后端·python·算法·性能优化·架构
丑梨数据10 小时前
信创环境里跑文档工具:格式兼容与本地合规的几个坑
数据结构·人工智能·算法·决策树·排序算法
erzong23112 小时前
Linux操作系统:线程概念与控制
linux·算法
Nil20813 小时前
leetcode 5最长回文子串
算法·leetcode·职场和发展
xxwl58513 小时前
区域赛分类题解
c++·笔记·学习·算法
m0_6745719713 小时前
力扣 hot 100 python
python·算法·leetcode
青少儿编程课堂13 小时前
蓝桥杯+全国青少年信息素养大赛+电子学会PythonC/C++考级:最长公共子序列LCS解析
c++·python·算法·bfs·信息学竞赛