首页 > 牛客编程巅峰赛S2赛季第5场代码第三题
头像
投简历⇆面试
编辑于 2020-12-01 21:33
+ 关注

牛客编程巅峰赛S2赛季第5场代码第三题

class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
* 给定一个后缀表达式,返回它的结果
* @param str string字符串
* @return
long长整型
*/
long long solve(string str) {
// write code here
long long a,b=1,c,d=0;
stack<long long> s;
for(int i=0;i<str.size();i++){
if(str[i]>='0'&&str[i]<='9'&&b==1){
a=str[i]-'0';
b=0;
d=0;
}
else if(str[i]>='0'&&str[i]<='9'&&b==0){
a*=10;
a+=str[i]-'0';
d=0;
}
else if(str[i]=='#'&&d==0){
b=1;
d=1;
s.push(a);
}
else if(str[i]=='+'){
a=s.top();
s.pop();
c=s.top();
s.pop();
s.push(a+c);
}
else if(str[i]=='-'){
a=s.top();
s.pop();
c=s.top();
s.pop();
s.push(c-a);
}
else if(str[i]=='*'){
a=s.top();
s.pop();
c=s.top();
s.pop();
s.push(a*c);
}
}
return s.top();
}
};

全部评论

(0) 回帖
加载中...
话题 回帖

推荐话题

相关热帖

近期热帖

近期精华帖

热门推荐