【LeetCode】224. Basic Calculator
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的更多相关文章
- 【LeetCode】224. Basic Calculator 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 栈 参考资料 日期 题目地址:https://lee ...
- 【LeetCode】227. Basic Calculator II 解题报告(Python)
[LeetCode]227. Basic Calculator II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: h ...
- 【刷题-LeetCode】224. Basic Calculator
Basic Calculator Implement a basic calculator to evaluate a simple expression string. The expression ...
- 【LeetCode】227. Basic Calculator
Problem: Implement a basic calculator to evaluate a simple expression string. The expression string ...
- 【LeetCode】227. Basic Calculator II
Basic Calculator II Implement a basic calculator to evaluate a simple expression string. The express ...
- 【LeetCode】991. Broken Calculator 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【leetcode】991. Broken Calculator
题目如下: On a broken calculator that has a number showing on its display, we can perform two operations ...
- leetcode 224. Basic Calculator 、227. Basic Calculator II
这种题都要设置一个符号位的变量 224. Basic Calculator 设置数值和符号两个变量,遇到左括号将数值和符号加进栈中 class Solution { public: int calcu ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
随机推荐
- python3 文件及文件夹路径相关
1. #返回当前文件所在的目录 currentDir = path.dirname(__file__) # __file__ 为当前文件 2.获得某个路径的父级目录: parent_path = os ...
- 【ElasticSearch】ElasticSearch-SQL插件
ElasticSearch-SQL插件 image2017-10-27_11-10-53.png (1067×738) elastic SQL_百度搜索 Druid SQL 解析器的解析过程 - be ...
- discuz上传头像失败怎么解决
刚安装好的discuz程序,可能需要我们做许多修改,而头像上传失败则是最为常见的问题之一,那么discuz上传头像失败怎么解决呢 进入ftp,打开跟目录下config文件 下载"config ...
- python连接mysql实例分享_python
示例一 #coding=UTF-8 import sys import MySQLdb import time reload(sys) sys.setdefaultencoding('utf-8') ...
- Android输出日志Log类
android.util.Log常用的方法有以下5个: Log.v() Log.d() Log.i() Log.w() 以及 Log.e().根据首字母分别对应VERBOSE,DEBUG,INFO,W ...
- requestFeature() must be called before adding content产生原因和解决办法
03-24 01:07:31.504 2957-2957/com.santai.jrj E/AndroidRuntime: FATAL EXCEPTION: main Process: com.san ...
- Java通过ScriptEngine 执行js脚本案例
public static void main(String[] args) throws ScriptException, FileNotFoundException, NoSuchMethodEx ...
- Serializable 介绍
今天咱们简单介绍一些serializable. 1.序列化是干什么的? 简单说就是为了保存在内存中的各种对象的状态(也就是实例变量,不是方法),并且可以把保存的对象状态再读出来.虽然你可以用你自己的各 ...
- JDK5.0特性-线程 Callable和Future
来自:http://www.cnblogs.com/taven/archive/2011/12/17/2291466.html import java.util.concurrent.Callable ...
- python获取系统开机时间
import psutil import time time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(psutil.boot_time()))