[Oracle] LeetCode 227 Basic Calculator II
Given a string s
which represents an expression, evaluate this expression and return its value.
The integer division should truncate toward zero.
You may assume that the given expression is always valid. All intermediate results will be in the range of [\(-2^{31}, 2^{31} - 1\)].
Note: You are not allowed to use any built-in function which evaluates strings as mathematical expressions, such as eval()
.
Solution
点击查看代码
class Solution {
public:
int calculate(string s) {
vector<int> vt;
int num=0;
char sign='+';
for(int i=0;i<=s.length();i++){
if(s[i]>='0'&&s[i]<='9'){
num=num*10+(s[i]-'0');
}else if(s[i]=='+'||s[i]=='-'||s[i]=='/'||s[i]=='*'||i==s.length()){
if(sign=='+'){
vt.push_back(num);
num=0;
}else if(sign=='-'){
vt.push_back(-1*num);
num=0;
}else if(sign=='/'){
vt[vt.size()-1]=vt.back()/num;
num=0;
}else if(sign=='*'){
vt[vt.size()-1]=vt.back()*num;
num=0;
}
if(i!=s.length()){
sign=s[i];
}
}
}
int result=0;
for(int i=0;i<vt.size();i++){
result+=vt[i];
}
return result;
}
};