Saturday, September 23, 2017
682. Baseball Game
int calPoints(vector<string>& ops) {
int res = 0;
stack<int> pts;
for(int i=0; i<ops.size(); i++) { // initialize i = 0
if(ops[i] == "C") {
int s = pts.top();
res -= s;
pts.pop();
}
else if(ops[i] == "D") {
int s = 2 * pts.top();
res += s;
pts.push(s);
}
else if(ops[i] == "+") {
int s1 = pts.top();
pts.pop();
int s2 = pts.top() + s1;
res += s2;
pts.push(s1);
pts.push(s2);
}
else {
int s = atoi(ops[i].c_str());
res += s;
pts.push(s);
}
}
return res;
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment