Java Stack Solution


  • 0
    H
     public int calPoints(String[] ops) {
    	  Stack<Integer> stack = new Stack<Integer>();
    	     int sum = 0;
    	     for(int i=0; i < ops.length; i++){
    	    	 if(ops[i].equals("C")){
    	    		 if(!stack.isEmpty()) {
    	    			 sum -= stack.peek();
    	    			 stack.pop();
    	    		 }
    	    	 } else if(ops[i].equals("D")){
    	    		 if(!stack.isEmpty()) {
    	    			 int elem = 2 * stack.peek();
    	    			 sum += elem;
    	    			 stack.push(elem);
    	    		 }
    	    	} else if(ops[i].equals("+")){
    	    			 if(!stack.isEmpty()) {
    	    				 int firstelem = stack.pop();
    	    				 int secondelem = stack.peek();
    	    				 int newelem = firstelem + secondelem;
    	    				 sum  += newelem;
    	    				 stack.push(firstelem);
    	    				 stack.push(newelem);
    	    			 }
    	    	} else {// Integer in the form of string
    	    			 sum += Integer.parseInt(ops[i]);
    	    			 stack.push(Integer.parseInt(ops[i]));
    	    		 }
    	    	 }
    	     return sum;
        }

Log in to reply
 

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