Baseball Game


  • 1

    Click here to see the full article post


  • 1

    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;
    }


  • 0
    W

    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.


  • 0

    @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.


  • 0
    T

    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;
    }
    };


  • 0

    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;
    };
    

  • 0
    C

    Won't is get EmptyStackException?


  • 0
    M

    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("+")){
    	        score.add(score.get(score.size()-1)+score.get(score.size()-2));
    		}else if(c.equals("D")){
    			score.add(score.get(score.size()-1)*2);
    		}else if(c.equals("C")){
    			score.remove(score.size()-1);
    		}else{
    			score.add(Integer.valueOf(c));
    		}
    	}
    
        
    	for(Integer s : score){
            sum += s;
        }
        
        
        return sum;
    }
    

    }


  • 0
    D

    #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;
    

    }


Log in to reply
 

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