这道题是只有四则运算但是没有括号的,因此可以利用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的更多相关文章

  1. LeetCode227:Basic Calculator II

    Implement a basic calculator to evaluate a simple expression string. The expression string contains ...

  2. Basic Calculator,Basic Calculator II

    一.Basic Calculator Total Accepted: 18480 Total Submissions: 94750 Difficulty: Medium Implement a bas ...

  3. LeetCode 227. 基本计算器 II(Basic Calculator II)

    227. 基本计算器 II 227. Basic Calculator II 题目描述 实现一个基本的计算器来计算一个简单的字符串表达式的值. 字符串表达式仅包含非负整数,+,-,*,/ 四种运算符和 ...

  4. [Swift]LeetCode227. 基本计算器 II | Basic Calculator II

    Implement a basic calculator to evaluate a simple expression string. The expression string contains ...

  5. Atitit HTTP 认证机制基本验证 (Basic Authentication) 和摘要验证 (Digest Authentication)attilax总结

    Atitit HTTP认证机制基本验证 (Basic Authentication) 和摘要验证 (Digest Authentication)attilax总结 1.1. 最广泛使用的是基本验证 ( ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. hosts.allow和hosts.deny支持哪些服务

    一.背景简介 在linux上多用iptables来限制ssh和telnet,编缉hosts.allow和hosts.deny感觉比较麻烦比较少用. aix没有iptables且和linux有诸多不同, ...

  2. python(2)之列表

    列表的使用 names=["zhangyang","liming",["sese","popo"],"xiao ...

  3. Minimum supported Gradle version is 4.1. Current version is 2.14.1

    Error:Minimum supported Gradle version is 4.1. Current version is 2.14.1. If using the gradle wrappe ...

  4. pickle file in matlab

    Load pickle files in Matlab Posted on June 12, 2013 by xcorr   http://xcorr.net/2013/06/12/load-pick ...

  5. 关于静态资源是否应该放到WEB-INF目录

    首先,css/js/html没有必要放在WEB-INF下. 最终这些会被原封不动的展现在客户端,所以访问安全根本就不会成为问题. jsp放在web-inf下,原因主要有两个 远古时代的模式会把业务逻辑 ...

  6. Bootstrap--思维导图

    Bootstrap--思维导图

  7. eclipse配置和使用memory Analyse分析内存

    1. 安装 在Eclipse help -> Eclipse Marketplace下搜索Memory:  图 1-1 搜索MAT插件 按照步骤安装完成重启即可. 2. 测试代码准备 测试代码 ...

  8. Linux系统管理常用命令用法总结(2)

    1.free指令会显示内存的使用情况,包括实体内存,虚拟的交换文件内存,共享内存区段,以及系统核心使用的缓冲区等. 语法:free [-bkmotV][-s <间隔秒数>] 参数说明: - ...

  9. CentOS7调整home盘空间到根目录

    1:解除挂载 umount /home 如报错: [root@zabbix-hk-01 home]# umount /home umount: /home:目标忙. (有些情况下通过 lsof(8) ...

  10. DevExpress v18.1新版亮点——XAF篇(一)

    用户界面套包DevExpress v18.1日前正式发布,本站将以连载的形式为大家介绍各版本新增内容.本文将介绍了DevExpress eXpressApp Framework(XAF) v18.1 ...