Input ["5","-2","4","C","D","9","+","+"]
Output 27
package stack_queue;
import java.util.*;
public class BaseballGame {
public static void main(String[] args){
String[] strs = {"5","-2","4","C","D","9","+","+"};
System.out.println("결과 값 : "+points(strs));
}
public static int points(String[] strs){
//1 데이터를 담을 수 있는 곳
Stack<Integer> stack = new Stack<>();
//2 데이터 뽑기
for(String op : strs){
switch(op) {
case "C" : //맨 뒤 제거
stack.pop();
break;
case "D" : //앞의 원소를 2배 후 삽입
stack.push(stack.peek()*2);
break;
case "+" :
int x = stack.pop();
int y = stack.pop();
stack.push(y);
stack.push(x);
stack.push(x+y);
break;
default :
stack.push(Integer.valueOf(op));
}
}
int sum=0;
while(!stack.isEmpty()){
sum+= stack.pop();
}
return sum;
}
}