leetcode227. Basic CalculatorII
这道题是只有四则运算但是没有括号的,因此可以利用stack来存储的,并且每次使得存储的值与符号有对应的关系,最后在栈中只剩下可以利用加法进行处理的数的,注意在i=n-1的时候到达最后的部分也是需要把数字压入栈中的而不能被忽略掉。
class Solution {
public:
int calculate(string s) {
int res=,num=,n=s.size();
stack<int> st;
char op='+';
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=='*'){
int temp=st.top()*num;
//cout<<temp<<endl;
st.pop();
st.push(temp);
}
if(op=='/'){
int temp=st.top()/num;
st.pop();
st.push(temp);
}
op=s[i];
num=;
//cout<<st.top()<<endl;
}
}
while(!st.empty()){
res+=st.top();
st.pop();
}
return res;
}
};
leetcode227. Basic CalculatorII的更多相关文章
- LeetCode227:Basic Calculator II
Implement a basic calculator to evaluate a simple expression string. The expression string contains ...
- Basic Calculator,Basic Calculator II
一.Basic Calculator Total Accepted: 18480 Total Submissions: 94750 Difficulty: Medium Implement a bas ...
- LeetCode 227. 基本计算器 II(Basic Calculator II)
227. 基本计算器 II 227. Basic Calculator II 题目描述 实现一个基本的计算器来计算一个简单的字符串表达式的值. 字符串表达式仅包含非负整数,+,-,*,/ 四种运算符和 ...
- [Swift]LeetCode227. 基本计算器 II | Basic Calculator II
Implement a basic calculator to evaluate a simple expression string. The expression string contains ...
- Atitit HTTP 认证机制基本验证 (Basic Authentication) 和摘要验证 (Digest Authentication)attilax总结
Atitit HTTP认证机制基本验证 (Basic Authentication) 和摘要验证 (Digest Authentication)attilax总结 1.1. 最广泛使用的是基本验证 ( ...
- Basic Tutorials of Redis(9) -First Edition RedisHelper
After learning the basic opreation of Redis,we should take some time to summarize the usage. And I w ...
- Basic Tutorials of Redis(8) -Transaction
Data play an important part in our project,how can we ensure correctness of the data and prevent the ...
- Basic Tutorials of Redis(7) -Publish and Subscribe
This post is mainly about the publishment and subscription in Redis.I think you may subscribe some o ...
- Basic Tutorials of Redis(6) - List
Redis's List is different from C#'s List,but similar with C#'s LinkedList.Sometimes I confuse with t ...
随机推荐
- js实现页面与页面之间传值的几种方法优劣
1. cookie 传值, 缺点: cookie储存是需要服务器支持的,本地直接运行静态文件是实现不了的 <script> //添加 cookie function cp_add_cook ...
- bash 调试
bashdb test.sh step edit visual 跳到那一行 R restart http://www.ibm.com/developerworks/cn/linux/l-cn-she ...
- zabbix3.4.7集成grafana详细步骤
打开官方网站下载grafana并安装 wget https://s3-us-west-2.amazonaws.com/grafana-releases/release/grafana-5.0.4-1. ...
- POJ 2373 Dividing the Path(DP + 单调队列)
POJ 2373 Dividing the Path 描述 农夫约翰的牛发现,在他的田里沿着山脊生长的三叶草是特别好的.为了给三叶草浇水,农夫约翰在山脊上安装了喷水器. 为了使安装更容易,每个喷头必须 ...
- 逆袭之旅DAY15.东软实训.Oracle.约束、序列、视图、索引、用户管理、角色
2018-07-11 08:26:00 有某个学生运动会比赛信息的数据库,保存了如下的表: 运动员sporter表:(运动员编号sporterid,运动员姓名name,运动员性别sex,所属系dep ...
- 7.7 C++基本关联式容器
参考:http://www.weixueyuan.net/view/6404.html 总结: 基本的关联式容器主要有:set.multiset.map和multimap,这四种容器可以分为两组:ma ...
- rap使用手册
https://github.com/thx/RAP/wiki/user_manual_cn
- do while
do while结构的基本原理和while结构是基本相同的,但是它保证循环体至少被执行一次.因为它是先执行代码,后判断条件,如果条件为真,继续循环.
- oracle中有关初始化参数文件的几个视图对比
涉及oracle中有关初始化参数文件的几个视图主要有:v$paraemter,v$parameter2,v$system_parameter,v$system_parameter2,v$spparam ...
- 解决The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers.
SpringMVC中当在浏览器中输入对应的MappingUrl时,报The resource identified by this request is only capable of generat ...