본문 바로가기
Problem Solving

[프로그래머스] L1 다트 게임 / 2018 카카오 블라인드 채용 (Java)

by JYHAN 2020. 12. 19.

카카오

[프로그래머스] L1 다트 게임 / 2018 카카오 블라인드 채용

[풀이]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import java.util.*;
class Solution {
    public int solution(String dR) {
        int answer = 0;
        Stack<Integer> stack = new Stack<Integer>();
        for(int i=0; i<dR.length(); i++){
            char c = dR.charAt(i);
            if(c=='1' && dR.charAt(i+1)=='0'){
                stack.push(10);
                i++;
            } else if('0'<=&& c<='9'){
                stack.push(c-'0');
            } else if(c=='D'){
                int top = stack.pop();
                stack.push(top*top);
            } else if(c=='T'){
                int top = stack.pop();
                stack.push(top*top*top);
            } else if(c=='*'){
                int top1 = stack.pop();
                top1 *= 2;
                if(!stack.empty()){
                    int top2 = stack.pop();
                    top2 *= 2;
                    stack.push(top2);
                }
                stack.push(top1);
            } else if(c=='#'){
                int top = stack.pop();
                top *= -1;
                stack.push(top);
            }
        }
        while(!stack.empty())
            answer += stack.pop();
        return answer;
    }
}
cs

댓글