images\cherry_red.png1 Leetcodegaogaotwo
      images\cherry_blue.png1.1 链表
         images\cherry_orange.png1.1.1 分割链表
         images\cherry_orange.png1.1.2 反转链表
         images\cherry_orange.png1.1.3 快慢指针 链表的中间节点
         images\cherry_orange.png1.1.4 链表的倒数第k个节点
         images\cherry_orange.png1.1.5 回文链表
         images\cherry_orange.png1.1.6 链表的删除
         images\cherry_orange.png1.1.7 链表反转2
         images\cherry_orange.png1.1.8 链表的第一个相交节点
      images\cherry_blue.png1.2 dfs
         images\cherry_orange.png1.2.1 岛屿数量
         images\cherry_orange.png1.2.2 机器人的运动范围
         images\cherry_orange.png1.2.3 字符串的全排列(回溯算法)
         images\cherry_orange.png1.2.4 判断一个数字是否可以表示成三的幂的和
         images\cherry_orange.png1.2.5 不同路径III dfs+回溯
         images\cherry_orange.png1.2.6 组合总和 dfs+回溯
         images\cherry_orange.png1.2.7 子集 dfs or 状态枚举
      images\cherry_blue.png1.3 bfs
      images\cherry_blue.png1.4 双指针
         images\cherry_orange.png1.4.1 奇数在前,偶数在后
      images\cherry_blue.png1.5 二维矩阵的前缀和
         images\cherry_orange.png1.5.1 1074. 元素和为目标值的子矩阵数量
         images\cherry_orange.png1.5.2 304. 二维区域和检索 - 矩阵不可变
      images\cherry_blue.png1.6 数组变化
         images\cherry_orange.png1.6.1 大数加减法
         images\cherry_orange.png1.6.2 717. 1 比特与 2 比特字符
      images\cherry_blue.png1.7 排序算法
         images\cherry_orange.png1.7.1 把数组排成最小的数
         images\cherry_orange.png1.7.2 根据字符出现的次数频率进行排序
         images\cherry_orange.png1.7.3 最大数
         images\cherry_orange.png1.7.4 969. 煎饼排序
      images\cherry_blue.png1.8 dp动态规划
         images\cherry_orange.png1.8.1 斐波那契数列
         images\cherry_orange.png1.8.2 俄罗斯套娃信封问题
         images\cherry_orange.png1.8.3 分割回文串
         images\cherry_orange.png1.8.4 01背包
         images\cherry_orange.png1.8.5 打家劫舍II
         images\cherry_orange.png1.8.6 青蛙过河
         images\cherry_orange.png1.8.7 最长回文子串
         images\cherry_orange.png1.8.8 目标和 (01背包)
         images\cherry_orange.png1.8.9 最大子序和
      images\cherry_blue.png1.9 滑动窗口
         images\cherry_orange.png1.9.1 绝对差不超过限制的最长连续子数组
         images\cherry_orange.png1.9.2 三数之和
      images\cherry_blue.png1.10 二叉树性质
         images\cherry_orange.png1.10.1 二叉树的最小深度
         images\cherry_orange.png1.10.2 二叉树的层次遍历
         images\cherry_orange.png1.10.3 二叉树的镜像翻转
         images\cherry_orange.png1.10.4 二叉树的最大深度
         images\cherry_orange.png1.10.5 二叉搜索树节点最小距离
         images\cherry_orange.png1.10.6 二叉树重构从小到达 按照->right情况
         images\cherry_orange.png1.10.7 二叉搜索数的范围和
         images\cherry_orange.png1.10.8 N叉树的前序遍历
      images\cherry_blue.png1.11 递归
         images\cherry_orange.png1.11.1 1+2...+n
      images\cherry_blue.png1.12 并查集
         images\cherry_orange.png1.12.1 并查集连通分量
         images\cherry_orange.png1.12.2 并查集城市连通
      images\cherry_blue.png1.13 堆栈
         images\cherry_orange.png1.13.1 通过队列来实现栈
         images\cherry_orange.png1.13.2 下一个更大元素 II
         images\cherry_orange.png1.13.3 删除字符串中的所有相邻重复项
         images\cherry_orange.png1.13.4 基本计算机
         images\cherry_orange.png1.13.5 计算机
         images\cherry_orange.png1.13.6 逆波兰表达式
         images\cherry_orange.png1.13.7 接雨水
         images\cherry_orange.png1.13.8 柱状图中最大矩形面积
         images\cherry_orange.png1.13.9 反转每对括号间的子串
      images\cherry_blue.png1.14 队列(or 优先队列)
         images\cherry_orange.png1.14.1 最大平均通过率
         images\cherry_orange.png1.14.2 最大的团队表现值
         images\cherry_orange.png1.14.3 找出第K大的异或坐标值
         images\cherry_orange.png1.14.4 1.2滑动窗口的最大值
         images\cherry_orange.png1.14.5 二叉搜索数的范围和
         images\cherry_orange.png1.14.6 员工的重要性
      images\cherry_blue.png1.15 Hash查询匹配
         images\cherry_orange.png1.15.1 猜灯谜
         images\cherry_orange.png1.15.2 720. 词典中最长的单词
      images\cherry_blue.png1.16 前缀树
         images\cherry_orange.png1.16.1 实现Trie(前缀树)
         images\cherry_orange.png1.16.2 添加与搜索单词 - 数据结构设计
      images\cherry_blue.png1.17 位运算
         images\cherry_orange.png1.17.1 比特位计数
         images\cherry_orange.png1.17.2 不用加减乘除做加法
         images\cherry_orange.png1.17.3 可被 5 整除的二进制前缀
         images\cherry_orange.png1.17.4 汉明距离总和
      images\cherry_blue.png1.18 二分法
         images\cherry_orange.png1.18.1 在D天内送达包裹的能力
         images\cherry_orange.png1.18.2 第一个错误的版本
      images\cherry_blue.png1.19 数据结构性质
      images\cherry_blue.png1.20 回溯法思想
      images\cherry_blue.png1.21 字符串处理(模拟)
         images\cherry_orange.png1.21.1 Excel 表列序号进制转换
         images\cherry_orange.png1.21.2 838.推多米诺
      images\cherry_blue.png1.22 模拟
      images\cherry_blue.png1.23 数学定理
         images\cherry_orange.png1.23.1 1828. 统计一个圆中点的数目
224. 基本计算器
实现一个基本的计算器来计算一个简单的字符串表达式 s 的值。



示例 1:

输入:s = "1 + 1"
输出:2
示例 2:

输入:s = " 2-1 + 2 "
输出:3
示例 3:

输入:s = "(1+(4+5+2)-3)+(6+8)"
输出:23


提示:

1 <= s.length <= 3 * 105
s 由数字、'+'、'-'、'('、')'、和 ' ' 组成
s 表示一个有效的表达式
通过次数42,872提交次数104,896


class Solution {
public:
    stack<long>;    //存储数字
    stack<char>t;   //储存符号

    int calculate(string s) {
        int m = s.length();
        long tag = -1;
        n.push(0);
        for(int i = 0;i<m;i++){
            if(s[i]==' '){          //当遇到空格的情况下
                continue;
            }

            if(s[i]>='0'&&s[i]<='9'){    //当遇到数字的情况下 注意数字并不一定是个位的 需要累加和并且入栈
                if(tag == -1){
                    tag = s[i]-'0';
                }else{
                    tag = tag * 10 +s[i]-'0';
                }
            }
            
            else{
                if(tag!=-1){            //是否为数字 并将数字入栈
                    n.push(tag);
                    tag = -1;
                }                       
                if(s[i]=='+'||s[i]=='-'){               //处理符号情况
                    while(!t.empty()){
                        if(t.top()=='('){
                            break;
                        }
                        long num1 = n.top();             //s每次弹出两个数字 t 弹出一个符号
                        n.pop(); 
                        long num2 = n.top();
                        n.pop();

                        if(t.top()=='+'){               //将运算的结果入栈
                            n.push(num1+num2);
                            t.pop();  
                        }else{
                            n.push(num2-num1);
                            t.pop();
                        }
                    }
                    t.push(s[i]);                       //当前符号入栈
                }
                else if(s[i]=='('||s[i]==')')
                {                                  //遇见( ) 情况
                    if(s[i]=='('){
                        t.push(s[i]);
                    }
                    if(s[i]==')'){
                        while(t.top()!='('){            //不断的进行运算
                            long num1 = n.top();             //s每次弹出两个数字 t 弹出一个符号
                                n.pop(); 
                            long num2 = n.top();
                                n.pop();
                            if(t.top()=='+'){               //将运算的结果入栈
                                n.push(num1+num2);
                                t.pop();  
                            }
                            else{
                                n.push(num2-num1);
                                t.pop();
                            }
                        }
                        t.pop();                        //弹出 (
                    }
                }

            }
        }

        if(tag!=-1){
            n.push(tag);
        }
        
        while(!t.empty()){
            long num1 = n.top();             //s每次弹出两个数字 t 弹出一个符号
            n.pop(); 
            long num2 = n.top();
            n.pop();
                if(t.top()=='+'){               //将运算的结果入栈
                    n.push(num1+num2);
                    t.pop();  
                }else{
                    n.push(num2-num1);
                    t.pop();
                }
        }
        return n.top();
    }
};