Basic Calculator

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、有括号时,先执行括号中的

两个栈:

一个存放操作数,每次进栈要注意,如果操作符栈顶元素为'+'/'-',则需要立即计算。

一个存放操作符(包括括号),每次出现')'时,不断进行出栈计算再进栈,直到弹出'(',说明当前括号内计算完毕。

class Solution {
public:
int calculate(string s) {
stack<int> num;
stack<int> op;
int i = ;
while(i < s.size())
{
while(i < s.size() && s[i] == ' ')
i ++;
if(i == s.size())
break;
if(s[i] == '+' || s[i] == '-' || s[i] == '(')
{
op.push(s[i]);
i ++;
}
else if(s[i] == ')')
{
while(op.top() != '(')
{// calculation within parentheses
int n2 = num.top();
num.pop();
int n1 = num.top();
num.pop();
if(op.top() == '+')
num.push(n1 + n2);
else
num.push(n1 - n2);
op.pop();
}
op.pop();
while(!op.empty() && op.top() != '(')
{
int n2 = num.top();
num.pop();
int n1 = num.top();
num.pop();
if(op.top() == '+')
num.push(n1 + n2);
else
num.push(n1 - n2);
op.pop();
}
i ++;
}
else
{
int n = ;
while(i < s.size() && s[i] >= '' && s[i] <= '')
{
n = n* + (s[i]-'');
i ++;
}
num.push(n);
while(!op.empty() && op.top() != '(')
{
int n2 = num.top();
num.pop();
int n1 = num.top();
num.pop();
if(op.top() == '+')
num.push(n1 + n2);
else
num.push(n1 - n2);
op.pop();
}
}
}
return num.top();
}
};

【LeetCode】224. Basic Calculator的更多相关文章

  1. 【LeetCode】224. Basic Calculator 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 栈 参考资料 日期 题目地址:https://lee ...

  2. 【LeetCode】227. Basic Calculator II 解题报告(Python)

    [LeetCode]227. Basic Calculator II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: h ...

  3. 【刷题-LeetCode】224. Basic Calculator

    Basic Calculator Implement a basic calculator to evaluate a simple expression string. The expression ...

  4. 【LeetCode】227. Basic Calculator

    Problem: Implement a basic calculator to evaluate a simple expression string. The expression string ...

  5. 【LeetCode】227. Basic Calculator II

    Basic Calculator II Implement a basic calculator to evaluate a simple expression string. The express ...

  6. 【LeetCode】991. Broken Calculator 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  7. 【leetcode】991. Broken Calculator

    题目如下: On a broken calculator that has a number showing on its display, we can perform two operations ...

  8. leetcode 224. Basic Calculator 、227. Basic Calculator II

    这种题都要设置一个符号位的变量 224. Basic Calculator 设置数值和符号两个变量,遇到左括号将数值和符号加进栈中 class Solution { public: int calcu ...

  9. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

随机推荐

  1. fastjson生成json时Null属性不显示 (转)

    http://blog.csdn.net/u010648555/article/details/51422340 null对应的key已经被过滤掉:这明显不是我们想要的结果,这时我们就需要用到fast ...

  2. 【转】深入理解line-height

    原文: http://www.cnblogs.com/dolphinX/p/3236686.html https://www.cnblogs.com/yangjie-space/p/4858132.h ...

  3. [Spring boot] Read values from external properties file

    Let's say we have a extral app.proporites file which contains some extra configuration: // resources ...

  4. (算法)等概率选出m个整数

    题目: 从大小为n的整数数组A中随机选出m个整数,要求每个元素被选中的概率相同. 思路: n选m,等概率情况下,每个数被选中的概率为m/n. 方法: 初始化:从A中选择前m个元素作为初始数组: 随机选 ...

  5. AWR - Load Profile 节

    AWR 报告的"Load profile"节,如下图所示,包含很多极为有用,却被经常忽视的信息.通常更倾向使用"instance efficiency percentag ...

  6. SDE注册版本失败,仅支持一个空间列

    如果直接编辑SDE要素类与要素可以不需要版本,使用默认版本,如果要让用户通过界面编辑,即使用开启编辑.保存编辑和停止编辑,就需要注册为版本,而在注册版本弹出如下错误: 正如错误所说,一个要素类或shp ...

  7. Asp.Net下通过切换CSS换皮肤

    直接重写Render事件 protected override void Render(System.Web.UI.HtmlTextWriter writer) { StringWriter sw = ...

  8. 微信小程序 - 提示消息组件

    配置挺简单的,也就不说明了,点击下载:alert

  9. Linux实现多线程高速下载

    使用Wget下载,有时候速度挺慢的. 有没有好办法呢? 一.解决方案 安装axel 安装方法: yum -y install epel-release .el7.x86_64.rpm rpm -ivh ...

  10. linux中fuser用法详解

    fuser功能 fuser 可以显示出当前哪个程序在使用磁盘上的某个文件.挂载点.甚至网络端口,并给出程序进程的详细信息.  fuser显示使用指定文件或者文件系统的进程ID.默认情况下每个文件名后面 ...