先算*/
让栈里只剩下+
最后求和即可
class Solution { public: int calculate(string s) { vector st; char presign = '+'; int num = 0; int n = s.size(); for(int i=0;i if(isdigit(s[i])) num = num*10 + (s[i] - '0'); if(!isdigit(s[i]) && s[i] != ' ' || i == n-1) { if(presign == '+') st.push_back(num); else if(presign == '-') st.push_back(-num); else if(presign == '/') st.back() /= num; else st.back() *= num; presign = s[i]; num = 0; } } return accumulate(st.begin(),st.end(),0); } };
下一篇:【LLMs】大语言模型分类