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 ...
随机推荐
- [转]10+倍性能提升全过程--优酷账号绑定淘宝账号的TPS从500到5400的优化历程
摘要: # 10+倍性能提升全过程--优酷账号绑定淘宝账号的TPS从500到5400的优化历程 ## 背景说明 > 2016年的双11在淘宝上买买买的时候,天猫和优酷土豆一起做了联合促销,在天猫 ...
- nyoj-0708-ones(dp)
nyoj-0708-ones 题意:用1,+,*,(,). 这四个符号组成表达式表达数s(0 <= s <= 10000),且1最少时1的的个数 状态转移方程: dp[i] = min(d ...
- Excel 数据读入到DataSet
using System; using System.Collections.Generic; using System.Linq; using System.Data; using System.I ...
- Cracking The Coding Interview4.5
//原文: // // Write an algorithm to find the 'next' node (i.e., in-order successor) of a given node in ...
- 3.1 C++继承的概念及语法
参考:http://www.weixueyuan.net/view/6358.html. 总结: 继承可以理解为一个类从另一个类获取方法(函数)和属性(成员变量)的过程. 被继承的类称为父类或基类,继 ...
- html页面技巧
Query获取Select选择的Text和Value: 语法解释: 1. $("#select_id").change(function(){//code...}); //为S ...
- UNICODE_STRING(用户模式 内核模式)
UNICODE_STRING结构: typedef struct _UNICODE_STRING { USHORT Length; ...
- centos7安装配置zabbix4.0
zabbix01 198.8.8.211 zabbix-server4.0 zabbix02 198.8.8.212 zabbix-agent4.0 一:zabbix服务端环境 ...
- Github拉取远端的时候提示“ssh: connect to host github.com port 22: Connection timed out”错误
在使用Github的时候,如果使用到拉取远端分支的时候或者测试ssh -T git@github.com的时候可能会出现连接失败的问题,错误描述为“ssh: connect to host githu ...
- oracle语句优化
摘录来自https://blog.csdn.net/sap_jack/article/details/3766703 1.选用适合的Oracle优化器 Oracle的优化器共有3种: a.RULE(基 ...