# Baseball Game

• Why don't you keep track of sum inside the same for loop instead of having a separate loop afterwards?
public int CalPoints(string[] ops) //with Stack
{
Stack<int> stack = new Stack<int>();
int sum = 0;
for (int i = 0; i < ops.Length; i++)
{
string s = ops[i];
switch (s)
{
case "+":
int temp = stack.Pop();
int n = stack.Peek() + temp;
sum += n;
stack.Push(temp);
stack.Push(n);
break;
case "D":
int m = stack.Peek()* 2;
sum += m;
stack.Push(m);
break;
case "C":
sum -= stack.Pop();
break;
default: //if integer
stack.Push(int.Parse(s));
sum += int.Parse(s);
break;
}
}
return sum;
}

• I wrote a solution using a normal array as a stack.
Taking a sum at the end is essentially fewer operations than keeping track of the sum during the for loop.

My initial submittion with summation at end took 7ms
My second submission with summation in for loop took 9ms.

• @yashar My reason wasn't related to waykole's "fewer operations" motivation, I just thought it was a simpler presentation for people trying to read the article and understand.

• wrote below :

class Solution {
public:
int calPoints(vector<string>& ops) {
int result = 0;
stack<int> mystack;
int sum = 0;
for (int i = 0; i < ops.size(); i++) {
if (ops.at(i) == "+") {
int a = mystack.top();
mystack.pop();
int b = mystack.top();
int r = a + b;
sum +=r;
mystack.push(a);
mystack.push(r);
} else if (ops.at(i) == "D") {
int a = mystack.top();
a = a << 1;
sum+=a;
mystack.push(a);
} else if (ops.at(i) == "C") {
int num = mystack.top();
sum-=num;
mystack.pop();
} else if ((0-30000) < stoi(ops.at(i)) && stoi(ops.at(i)) < 30000) {
int num = stoi(ops.at(i));
sum += num;
mystack.push(num);
}
}
return sum;
}
};

• JS solution

``````var calPoints = function(ops) {
let sum = 0, op_arr = [];
for(let i=0;i<ops.length;i++){
switch(ops[i]){
case '+':
sum += op_arr[op_arr.push(op_arr[op_arr.length-1] + op_arr[op_arr.length-2])-1];;
break;
case 'C':
sum -= op_arr.pop();
break;
case 'D':
sum += op_arr[op_arr.push(op_arr[op_arr.length-1] * 2)-1];
break;
default:
sum += op_arr[op_arr.push(parseInt(ops[i]))-1];
}
}
return sum;
};
``````

• Won't is get EmptyStackException?

• class Solution {
public int calPoints(String[] ops) {

``````    int sum = 0;

List<Integer> score = new ArrayList<Integer>();

for(int i=0;i<ops.length;i++){
String c = ops[i];
if(c.equals("+")){
}else if(c.equals("D")){
}else if(c.equals("C")){
score.remove(score.size()-1);
}else{
}
}

for(Integer s : score){
sum += s;
}

return sum;
}
``````

}

• #include <stdio.h>
#include <stdlib.h>

int main(void) {

``````char *str_buf = '\0';
int i=0,ctr=0,k;

str_buf = "Let's take LeetCode contest";//"Hello, How's your day";

while(str_buf[i]!='\0'){

if(str_buf[i]== ' '){
k=0;
while(k<=ctr){
printf("%c",str_buf[i-k]);
k++;
}
ctr=0;
}
else
ctr++;

i++;
}

if (str_buf[i-1] != ' ') {
k = 0;
printf(" ");
while (k <= ctr) {
printf("%c", str_buf[(i - 1) - k]);
k++;
}
}

return EXIT_SUCCESS;
``````

}

["30000","D","D","D",...,"D"] with 999 "D"

• ``````class Solution {
public int calPoints(String[] ops) {

Stack<Integer> stack = new Stack<>();

for (int i =0;i<ops.length;i ++){

if (ops[i].equals("+")){

int top = stack.pop();
int newTop = top+stack.peek();
stack.push(top);
stack.push(newTop);
}
else if (ops[i].equals("C")){
stack.pop();
}
else if (ops[i].equals("D")){
int top = stack.pop();
int newTop = 2*top;
stack.push(top);
stack.push(newTop);
}
else stack.push(Integer.valueOf(ops[i]));
}
while(!stack.isEmpty()){
}

}
}``````

• Simple Java solution which is somehow not giving desired output during submission, but is correct in my test cases:
int sum = 0;
Stack<Integer> stack = new Stack<>();
for(int i =0; i<ops.length;i++){
if(Character.isDigit(ops[i].charAt(0)) || ops[i].startsWith("-")){
stack.push(Integer.valueOf(ops[i]));
sum = sum + stack.peek();
}else
if(ops[i].equals("C")){
sum = sum - stack.peek();
stack.pop();
}else
if(ops[i].equals("D")){
stack.push(stack.peek() * 2);
sum = sum + stack.peek();
}else
if(ops[i] == "+"){
stack.push(stack.peek() + (stack.size()>1?stack.get(stack.size()-2):0));
sum = sum + stack.peek();
}
}
return sum;

• C++ Solution

``````public:
int calPoints(vector<string>& ops) {
vector<int> rounds;
for(int i = 0; i < ops.size(); i++){
if(ops[i] == "D")
rounds.push_back(rounds.back() * 2);
else if(ops[i] == "C")
rounds.pop_back();
else if(ops[i] == "+"){
int size = rounds.size();
rounds.push_back(rounds[size - 1] + rounds[size - 2]);
}
else
rounds.push_back(stoi(ops[i]));
}
return accumulate(rounds.begin(), rounds.end(), 0);
}
};``````

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.