682. Baseball Game 發表於 2023-02-13 | 分類於 leetcode problemsolution12345678910111213141516171819202122232425262728293031323334353637class Solution {public: int Convert(string str){ int pos = 1; int i =0; if(str[i] == '-') { pos = -1; i++; } int num = 0; for(i;i<str.size();++i) num = 10*num+str[i] -'0'; return pos*num; } int calPoints(vector<string>& ops) { vector<int> cur; for(string str:ops){ if(str == "C"){ cur.pop_back(); } else if(str=="D"){ cur.push_back(cur.back()*2); } else if(str=="+"){ int size = cur.size(); cur.push_back(cur[size-1]+cur[size-2]); } else{ //conver to int int num = Convert(str); cur.push_back(num); } } int total = 0; for(int a:cur) total+=a; return total; }}; analysis time complexity O(n) space complexity O(n)