leetcode_Basic Calculator II
题目:
Implement a basic calculator to evaluate a simple expression string.
The expression string contains only non-negative integers, +
, -
, *
, /
operators
and empty spaces . The integer division should truncate toward zero.
You may assume that the given expression is always valid.
Some examples:
"3+2*2" = 7
" 3/2 " = 1
" 3+5 / 2 " = 5
Note: Do not use the eval
built-in
library function.
Credits:
Special thanks to @ts for adding this problem and creating all test cases.
代码:
class Solution {
public:
int calculate(string s) {
stack<int> st;
int result = 0;
int i = 0;
while(i<s.size())
{
int sum = 0;
if(i<s.size()&&s.at(i)>='0'&&s.at(i)<='9')
{
while(i<s.size()&&s.at(i)>='0'&&s.at(i)<='9')
{
sum = sum * 10 + s.at(i)-'0';
i++;
}
st.push(sum);
}
else if(i<s.size()&&s.at(i)=='+')
{
st.push((int)s.at(i));
i++;
}
else if(i<s.size()&&s.at(i)=='-')
{
st.push((int)s.at(i));
i++; }
else if(i<s.size()&&s.at(i)=='*')
{
int a = st.top();
st.pop();
int sum1 = 0;
i++;
while(s.at(i)==' ')
{
i++;
}
if(i<s.size()&&s.at(i)>='0'&&s.at(i)<='9')
{
while(i<s.size()&&s.at(i)>='0'&&s.at(i)<='9')
{
sum1 = sum1 * 10 + s.at(i)-'0';
i++;
}
a*=sum1;
st.push(a);
}
}
else if(i<s.size()&&s.at(i)=='/')
{ int a = st.top();
st.pop();
int sum1 = 0;
i++;
while(s.at(i)==' ')
{
i++;
}
while(i<s.size()&&s.at(i)>='0'&&s.at(i)<='9')
{
sum1 = sum1 * 10 + s.at(i)-'0';
i++;
}
a = (int)(a/sum1);
st.push(a);
}
else
{
i++;
}
}
stack<int> st1;
while(!st.empty())
{
st1.push(st.top());
st.pop();
}
while(!st1.empty())
{
int a = st1.top();
st1.pop();
if(st1.empty())
return a;
else
{
if((char)(st1.top())=='+')
{
st1.pop();
int temp = st1.top()+a;
st1.pop();
st1.push(temp);
}
else if((char)(st1.top())=='-')
{
st1.pop();
int temp = a-st1.top();
st1.pop();
st1.push(temp);
}
}
}
return st1.top();
}
};
方法比较繁琐。。。就这样了。
leetcode_Basic Calculator II的更多相关文章
- Basic Calculator,Basic Calculator II
一.Basic Calculator Total Accepted: 18480 Total Submissions: 94750 Difficulty: Medium Implement a bas ...
- 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 ...
- [LeetCode] Basic Calculator & Basic Calculator II
Basic Calculator Implement a basic calculator to evaluate a simple expression string. The expression ...
- LeetCode OJ Basic Calculator II
Basic Calculator II 题目 思路 和这个一样:Basic Calculator I 代码 class ExpressionTransformation { public: strin ...
- LeetCode 227. 基本计算器 II(Basic Calculator II)
227. 基本计算器 II 227. Basic Calculator II 题目描述 实现一个基本的计算器来计算一个简单的字符串表达式的值. 字符串表达式仅包含非负整数,+,-,*,/ 四种运算符和 ...
- 【LeetCode】227. Basic Calculator II 解题报告(Python)
[LeetCode]227. Basic Calculator II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: h ...
- [LeetCode] Basic Calculator II 基本计算器之二
Implement a basic calculator to evaluate a simple expression string. The expression string contains ...
- Basic Calculator II
Implement a basic calculator to evaluate a simple expression string. The expression string contains ...
随机推荐
- FreeRTOS 消息队列
以下基础内容转载自安富莱电子: http://forum.armfly.com/forum.php 本章节为大家讲解 FreeRTOS 的一个重要的通信机制----消息队列,初学者要熟练掌握,因为消息 ...
- 如何利用wireshark对TCP消息进行分析
原文:https://www.cnblogs.com/studyofadeerlet/p/7485298.html 如何利用wireshark对TCP消息进行分析 (1) 几个概念介绍 1 seq ...
- oracle 远程登录
打开命令终端,输入命令:sqlplus /nolog 输入命令:conn sys/sys@dba as sysdba
- 基于CSS3 3D百叶窗图像过渡特效
你可能已经在网上看到过不少使用jQuery制作的百叶窗效果,我们可不可以使用纯CSS来完成这项工作呢?答案是肯定的.我们不仅可以制作出这种百叶窗效果,还可以使它具有响应性. 在线预览 源码下载 要 ...
- cocos2dx迷你地图
用CCRenderTexture就可以了,不知是否有更好的方法. if (!miniMap) { miniMap=CCSprite::create(); miniMap->setZOrder() ...
- beaglebone black ubuntu display x11 server的配置
Change default resolution on BeagleBone modesetting vs fbdev digiteltlc May 7th, 2014, 03:28 PM Hi ...
- Jquery右下角消息提示框
找了好久,终于找到一个不错的提示框jquery,免费给大家一起用下吧(可是花了我很多积分哦),顺便重写了一下样式,原来的样式真的不敢恭维啊... 下载地址:http://files.cnblogs.c ...
- CentOS下yum安装PostgreSQL
关键词:centos install PostgreSQL Configure YUM repository vim /etc/yum.repos.d/CentOS-Base.repo [base] ...
- avrdude: stk500_getsync(): not in sync: resp=0x00
avrdude: stk500_getsync(): not in sync: resp=0x00错误提示在arduino IDE中非常常见,这个错误代表着就是无法通过USB口与arduino控制板进 ...
- mysql实现经纬度计算两个坐标之间的距离sql语句
select *,(2 * 6378.137* ASIN(SQRT(POW(SIN(PI()*(111.86141967773438-latitude)/360),2)+COS(PI()*33.070 ...