#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
* 给定一个后缀表达式,返回它的结果
* @param str string字符串
* @return long长整型
*/
long long legalExp(string str) {
// write code here
vector<long long> a;
long long index = -1;
long long res;
for (int i =0; i<str.size(); i++) {
switch (str[i]) {
case '+':
res = a[a.size()-2] + a[a.size()-1];
a.pop_back();
a.pop_back();
a.push_back(res);
cout<<res<<endl;
index = i;
break;
case '-':
res = a[a.size()-2] - a[a.size()-1];
a.pop_back();
a.pop_back();
a.push_back(res);
cout<<res<<endl;
index = i;
break;
case '*':
res = a[a.size()-2] * a[a.size()-1];
a.pop_back();
a.pop_back();
a.push_back(res);
cout<<res<<endl;
index = i;
break;
case '#':
string numstr;
for (int j=index+1; j<i; j++) {
numstr.push_back(str[j]);
}
a.push_back(stoi(numstr));
index = i;
cout<<stoi(numstr)<<endl;
break;
}
}
return a[0];
}
};
为啥把‘#’放在最前面后面的所有case都会报错呢?

京公网安备 11010502036488号