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. HTML5 File API 全介绍

    在 HTML5 File API 出现之前,前端对于文件的操作是非常有局限性的,大多需要配合后端实现.出于安全角度考虑,从本地上传文件时,代码不可能获取文件在用户本地的地址,所以纯前端不可能完成一些类 ...

  2. 用Python开始机器学习(2:决策树分类算法)

    http://blog.csdn.net/lsldd/article/details/41223147 从这一章开始进入正式的算法学习. 首先我们学习经典而有效的分类算法:决策树分类算法. 1.决策树 ...

  3. (转)【风宇冲】Unity3D教程宝典之AssetBundles:第一讲

    自:http://blog.sina.com.cn/s/blog_471132920101gz8z.html 原创文章如需转载请注明:转载自风宇冲Unity3D教程学院                 ...

  4. 从头认识java-15.7 Map(7)-TreeMap与LinkedHashMap

    这一章节我们来讨论一下Map两个比較经常使用的实现:TreeMap与LinkedHashMap. 1.TreeMap 特性:依照key来排序 package com.ray.ch14; import ...

  5. 轻松python文本专题-字符与字符值转换

    场景: 将字符转换成ascii或者unicode编码 在转换过程中,注意使用ord和chr方法 >>> print(ord('a')) 97 >>> print(c ...

  6. OS 获取用户相册。保存图片。编辑图片为圆形

    // // ViewController.m // YunPhoto // // Created by qingyun on 3/4/14. // Copyright (c) 2014 qingyun ...

  7. Mina.Net实现的UDP多路广播

    主要用于未确定主机地址的情况下,可以使用多路广播和服务端通信,下面是官方提供的DEMO. using System; using System.Net; using System.Net.Socket ...

  8. MySql【Insert Select Not Exist】判断记录再添加值的方案

    INSERT INTO content ( detail, status, beginTime, endTime) SELECT @detail, , NULL, NULL FROM DUAL WHE ...

  9. Go语言中Socket通信TCP服务端

    1.用法: (1)定义远程IP地址.使用net.ResolveTCPAddr()方法,定义一个TCP地址,做为本机监听地址. (2)使用net.ListenTCP("tcp",lo ...

  10. Five Steps to Avoiding Java Heap Space Errors

    来自:https://www.mapr.com/blog/how-to-avoid-java-heap-space-errors-understanding-and-managing-task-att ...