Home 프로그래머스 [1차] 다트 게임
Post
Cancel

프로그래머스 [1차] 다트 게임

프로그래머스 1차 다트 게임

내가 푼 풀이

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
import java.util.*;

class Solution {
    public static int solution(String dartResult) {
        int answer = 0;
        int score = 0;
        int idx = 0;

        ArrayList<Integer> list = new ArrayList<>();

        for(int i=0; i<dartResult.length(); i++){
            char c = dartResult.charAt(i);

            if(c=='1' && dartResult.charAt(i+1) =='0'){
                score = 10;
                i++;
            }
            else if(c >= '0' && c <= '9'){ score = c - 48; }
            else if(c == 'S') { list.add(score); idx++; }
            else if(c == 'D') { list.add((int)Math.pow(score, 2)); idx++; }
            else if(c == 'T') { list.add((int)Math.pow(score, 3)); idx++; }
            else if(c == '*') {
                if(idx > 1)
                    list.set(idx-2,list.get(idx-2)*2);
                list.set(idx-1,list.get(idx-1)*2);
            }else if(c == '#'){ list.set(idx-1,-list.get(idx-1)); }
        }

        for(int num : list)
            answer+=num;


        return answer;
    }
}
This post is licensed under CC BY 4.0 by the author.