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 ...
随机推荐
- 查看MySQL的线程
通过两张表查看MySQL的线程:information_schema.processlist 和 performance_schema.threads processlist是information_ ...
- ACCESS数据库基本使用
ACCESS是Office自带的数据库,使用起来非常方便. 相比,其它数据库来说,使用率较低,但是同MYSQL一样,免费.正因为如此,所以很多建站的程序员,还是会选择使用它. 部分代码: <sc ...
- get url img
selenium 1● 了解selenium 这个是做 web页面测试,模拟用户测试 =====> pip install selenium 安装 2● django 用于接 ...
- dl简单模板,无pretraining过程
layer_dimensions = [11 22 33 22 11]'; ld_size = size(layer_dimensions , 1); % what is deal [x rx dx ...
- Win10系列:UWP界面布局进阶5
提示框 在Windows应用商店应用程序中可以使用提示框来向用户显示提示信息,例如可以通过对话框来询问用户当前需要执行的操作,还可以通过弹出窗口来显示需要注意的信息.本节将向读者介绍如何在Window ...
- ural1517
题解: 后缀数组 求一下最长公共字串 代码: #include<cstdio> #include<cmath> #include<algorithm> #inclu ...
- 一些做vue前端的经验
1.先赋值,后渲染 场景:表格渲染中,一般都是这样把json的东西传给table的 this.tableData = json.data.rows 然后的话我们一般会在渲染前对json中的数据做一些转 ...
- Grafana配置SingleStat图表信息(三)
Grafana是一个用于展示数据的工具,配置数据源,直接连接数据库.(这里的数据库应该是结果库,直接需要给用户看的结果数据信息) SingleStat : 想用来展示单一数据信息的图表,效果图如图 ( ...
- 深度学习caffe测试代码c++
#include <caffe/caffe.hpp> #include <opencv2/core/core.hpp> #include <opencv2/highgui ...
- 每天CSS学习之border-radius
css3的border-radius属性,我们用之来画圆角边框. 1.border-radius:none;//表示不用圆角边框,边框会变成方形. 2.border-radius:水平方向{1,4}[ ...