Basic Calculator II
该题和前面的"
Basic Calculator
"的处理方法一样,仅仅是增加了对"*"、"/"两种运算的支持。
class Solution {
public:bool isnum(char c){if(c >= '0' && c <= '9')return true;return false;}int calculate(string s) { vector<string> postorder;stack<char> ccache;stack<int> icache;string tmp;if(s.length() < 1)return 0;
//构造后缀表达式for(int i = 0; i < s.length(); ){if(s[i] == ' '){i++;continue;}if(!isnum(s[i])){if(s[i] == '(' || ccache.empty()){ccache.push(s[i]);i++;continue;}if(s[i] == ')'){while(ccache.top() != '('){tmp = "";tmp += ccache.top();postorder.push_back(tmp);ccache.pop();}ccache.pop();i++;continue;}if(s[i] == '+' || s[i] == '-'){while(!ccache.empty() && ccache.top() != '(' ){tmp = "";tmp += ccache.top();postorder.push_back(tmp);ccache.pop();}ccache.push(s[i]);i++;continue;}if(s[i] == '*' || s[i] == '/'){while(!ccache.empty() && ccache.top() != '(' && ccache.top() != '+' && ccache.top() != '-'){tmp = "";tmp += ccache.top();postorder.push_back(tmp);ccache.pop();}ccache.push(s[i]);i++;continue;}}else{int j = i;while(j < s.length() && isnum(s[j]))j++;tmp = "";tmp = s.substr(i, j - i);postorder.push_back(tmp);i = j;}}
//加入全部剩余的元素while(!ccache.empty()){tmp = "";tmp += ccache.top();ccache.pop();postorder.push_back(tmp);}
//通过后缀表达式计算结果值int fir, sec, result;for(int i = 0; i < postorder.size(); i++){if(postorder[i] == "+" || postorder[i] == "-" || postorder[i] == "*" || postorder[i] == "/"){sec = icache.top();icache.pop();fir = icache.top();icache.pop();if(postorder[i] == "+")result = fir + sec;if(postorder[i] == "-")result = fir - sec;if(postorder[i] == "*")result = fir * sec;if(postorder[i] == "/")result = fir / sec;icache.push(result);}else{icache.push(atoi(postorder[i].c_str()));}}return icache.top();}
};
转载于:https://www.cnblogs.com/clnchanpin/p/7049935.html
Basic Calculator II相关推荐
- LeetCode OJ Basic Calculator II
Basic Calculator II 题目 思路 和这个一样:Basic Calculator I 代码 class ExpressionTransformation { public:string ...
- 【LeetCode】Basic Calculator II
Basic Calculator II 问题描述 Implement a basic calculator to evaluate a simple expression string. The ex ...
- LeetCode Basic Calculator II(加减乘除计算器)
与 LeetCode Basic Calculator(用栈计算表达式的值)相似 代码如下: public class Solution {private int cal(int num1, int ...
- [Swift]LeetCode227. 基本计算器 II | Basic Calculator II
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ ➤微信公众号:山青咏芝(shanqingyongzhi) ➤博客园地址:山青咏芝(https://www.cnblog ...
- 227 Basic Calculator II
1 题目 Implement a basic calculator to evaluate a simple expression string.The expression string conta ...
- 227. Basic Calculator II
The expression string contains only non-negative integers, +, -, *, / operators and empty spaces. Th ...
- leetcode 227. Basic Calculator II | 227. 基本计算器 II(中缀表达式求值)
题目 https://leetcode.com/problems/basic-calculator-ii/ 题解 这道题是 中缀表达式求值 的简化版(因为没有左右括号运算),不过输入的形式有两个处理起 ...
- leetcode----227. Basic Calculator II
链接: https://leetcode.com/problems/basic-calculator-ii/ 大意: 实现一个计算器.给定一个字符串s,字符串s中的字符由'0'-'9'的数字以及'+' ...
- [LeetCode]Basic Calculator
题目:Basic Calculator 给定一个合法的运算表达式,该表达式中只包含数字.'+'.'-'.' '.'('.')'. 思路: 简单思考不用看成加减两种运算,直接看成加法,只不过由正负: 如 ...
最新文章
- 从0到1 | 手把手教你如何使用哈工大NLP工具——PyLTP!
- AI攻击AI,升级的网络安全战
- ASP.NET MVC教程:理解模型、视图和控制器(1)
- 软件架构设计原则和大数据平台架构层
- visio交换机图标_分享 | 华为交换机开局配置一本通,弱电新人学习!
- ReadWriteLock读写文件
- Sublime Text 2搭建Java开发环境
- 查看oracle大页,在Oracle11.2.0.3.0上开启大页(hugepages)的详细解析
- 若依前后端不分离项目上传文件或图片不能显示出来?
- Android通过堆栈信息解决ANR问题
- 17.TCP/IP 详解卷1 --- TCP: 传输控制协议
- java做安卓开发需要学什么,安卓开发要学什么 需要什么基础知识
- Python 身份证校验代码
- 0x0000006B蓝屏解决方法
- Bootstrap框架
- linux系统怎么连接显示器,Linux下笔记本外接显示器 · Eulerlee
- python不显示边框_python设置表格边框的具体方法
- T1114 白细胞计数(信息学一本通C++)
- 启示录:了解TOD模式的正确打开方式(一)
- 【滤波跟踪】基于EKF、时差和频差定位实现目标跟踪附matlab代码
热门文章
- ERROR: Failed building wheel for pycrypto
- 由于找不到mfc110.dll,无法继续执行代码的解决方法
- 红蓝对抗之如何利用Shellcode来躲避安全检测
- P4570 [BJWC2011]元素(线性基+贪心)
- pymysql 于pycharm中操作mysql
- 小甲鱼Python第二十二讲课后习题
- Spring 配置多个数据源,并实现动态切换
- mysql面试题分组并合并列
- BZOJ_1601_[Usaco2008_Oct]_灌水_(最小生成树_Kruskal)
- MapReduce:Job性能调优总结