[LeetCode] 227. Basic Calculator II 基本计算器之二
Implement a basic calculator to evaluate a simple expression string.
The expression string contains only non-negativeintegers, +, -, *, / operators and empty spaces . The integer division should truncate toward zero.
Example 1:
Input: "3+2*2"
Output: 7
Example 2:
Input: " 3/2 "
Output: 1
Example 3:
Input: " 3+5 / 2 "
Output: 5
Note:
- You may assume that the given expression is always valid.
- Do not use the
evalbuilt-in library function.
Credits:
Special thanks to @ts for adding this problem and creating all test cases.
这道题是之前那道 Basic Calculator 的拓展,不同之处在于那道题的计算符号只有加和减,而这题加上了乘除,那么就牵扯到了运算优先级的问题,好在这道题去掉了括号,还适当的降低了难度,估计再出一道的话就该加上括号了。不管那么多,这道题先按木有有括号来处理,由于存在运算优先级,我们采取的措施是使用一个栈保存数字,如果该数字之前的符号是加或减,那么把当前数字压入栈中,注意如果是减号,则加入当前数字的相反数,因为减法相当于加上一个相反数。如果之前的符号是乘或除,那么从栈顶取出一个数字和当前数字进行乘或除的运算,再把结果压入栈中,那么完成一遍遍历后,所有的乘或除都运算完了,再把栈中所有的数字都加起来就是最终结果了,参见代码如下:
解法一:
class Solution {
public:
int calculate(string s) {
long res = , num = , n = s.size();
char op = '+';
stack<int> st;
for (int i = ; i < n; ++i) {
if (s[i] >= '') {
num = num * + s[i] - '';
}
if ((s[i] < '' && s[i] != ' ') || i == n - ) {
if (op == '+') st.push(num);
if (op == '-') st.push(-num);
if (op == '*' || op == '/') {
int tmp = (op == '*') ? st.top() * num : st.top() / num;
st.pop();
st.push(tmp);
}
op = s[i];
num = ;
}
}
while (!st.empty()) {
res += st.top();
st.pop();
}
return res;
}
};
在做了 Basic Calculator III 之后,再反过头来看这道题,发现只要将处理括号的部分去掉直接就可以在这道题上使用,参见代码如下:
解法二:
class Solution {
public:
int calculate(string s) {
long res = , curRes = , num = , n = s.size();
char op = '+';
for (int i = ; i < n; ++i) {
char c = s[i];
if (c >= '' && c <= '') {
num = num * + c - '';
}
if (c == '+' || c == '-' || c == '*' || c == '/' || i == n - ) {
switch (op) {
case '+': curRes += num; break;
case '-': curRes -= num; break;
case '*': curRes *= num; break;
case '/': curRes /= num; break;
}
if (c == '+' || c == '-' || i == n - ) {
res += curRes;
curRes = ;
}
op = c;
num = ;
}
}
return res;
}
};
Github 同步地址:
https://github.com/grandyang/leetcode/issues/227
类似题目:
参考资料:
https://leetcode.com/problems/basic-calculator-ii/
https://leetcode.com/problems/basic-calculator-ii/discuss/63003/Share-my-java-solution
https://leetcode.com/problems/basic-calculator-ii/discuss/63004/17-lines-C++-easy-20-ms
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] 227. Basic Calculator II 基本计算器之二的更多相关文章
- [LeetCode] Basic Calculator II 基本计算器之二
Implement a basic calculator to evaluate a simple expression string. The expression string contains ...
- [LeetCode] 227. Basic Calculator II 基本计算器 II
Implement a basic calculator to evaluate a simple expression string. The expression string contains ...
- LeetCode#227.Basic Calculator II
题目 Implement a basic calculator to evaluate a simple expression string. The expression string contai ...
- Java for LeetCode 227 Basic Calculator II
Implement a basic calculator to evaluate a simple expression string. The expression string contains ...
- (medium)LeetCode 227.Basic Calculator II
Implement a basic calculator to evaluate a simple expression string. The expression string contains ...
- 【LeetCode】227. Basic Calculator II 解题报告(Python)
[LeetCode]227. Basic Calculator II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: h ...
- leetcode 224. Basic Calculator 、227. Basic Calculator II
这种题都要设置一个符号位的变量 224. Basic Calculator 设置数值和符号两个变量,遇到左括号将数值和符号加进栈中 class Solution { public: int calcu ...
- 【LeetCode】227. Basic Calculator II
Basic Calculator II Implement a basic calculator to evaluate a simple expression string. The express ...
- 224. Basic Calculator + 227. Basic Calculator II
▶ 两个四则表达式运算的题目,第 770 题 Basic Calculator IV 带符号计算不会做 Orz,第 772 题 Basic Calculator III 要收费 Orz. ▶ 自己的全 ...
随机推荐
- 迷你版mybatis
public class BootStrap { public static void start(){ MySqlSession sqlSession = new MySqlSession();// ...
- 我是如何理解并使用maven的
前言 一直想写一篇关于Maven的文章,但是不知如何下笔,如果说能使用,会使用Maven的话,一.两个小时足矣,不需要搞懂各种概念.那么给大家来分享下我是如何理解并使用maven的. 什么是Maven ...
- WEB测试应该注意哪些地方,怎样才能做好WEB测试
基于Web的系统测试与传统的软件测试既有相同之处,也有不同的地方,对软件测试提出了新的挑战.基于Web的系统测试不但需要检查和验证是否按照设计的要求运行,而且还要评价系统在不同用户的浏览器端的显示是否 ...
- 【01】Nginx:编译安装/动态添加模块
写在前面的话 说起 Nginx,别说运维,就是很多开发人员也很熟悉,毕竟如今已经 2019 年了,Apache 更多的要么成为了历史,要么成为了历史残留. 我们在提及 Nginx 的时候,一直在强调他 ...
- php 5.5 编译安装
链接:https://pan.baidu.com/s/1Iy5kdugWqmvtsrYG0WYAdA 提取码:knk9 上面的链接 php5.5.8 编译安装的包 ./configure --pre ...
- .net core 发布到iis问题 HTTP Error 500.30 - ANCM In-Process Start Failure
1. 没有在Program里配置IIS webBuilder.UseIIS(); 2. StartupProduction 里AutoFac容器注入错误和新版的CORS中间件已经阻止使用允许任意Ori ...
- mvc ajax跳转controller 的路径
mvc Controller : url: "../phone/index",(控制器名,方法名) 一般处理程序.ashx : url: "../bianji.ashx ...
- scrapy学习笔记(一)
环境:Windows 7 x64 Python3.7.1 pycharm 一.安装scrapy 1.1linux系统使用:pip install scrapy 1.2Windows系统: pi ...
- Ext.create使用(下)
本文介绍第三种使用方法: //通过类的引用实例化一个类 var w1 = Ext.create(Ext.window.Window, {//类的引用 title: '窗体', html:'<fo ...
- 基础系列(2)--- css1
css组成 css语法组成 选择器 和 声明 (多个声明用分号隔开) 声明包括 属性和属性值(多个属性值用空格隔开) 语法: 选择器{ 属性: 属性值; 属性: 属性值1 属性值2; } css样式表 ...