Hayden's Archive
[알고리즘] 프로그래머스 : [1차] 다트 게임 본문
알고리즘 문제 출처 : programmers.co.kr/learn/courses/30/lessons/17682
내가 작성한 코드
import java.util.ArrayList;
import java.util.List;
class Solution {
public int solution(String dartResult) {
int answer = 0;
String[] score = dartResult.split("");
String tempStr = "";
List<Integer> list = new ArrayList<>();
for(int i=0; i<score.length; i++) {
if(48 <= score[i].charAt(0) && score[i].charAt(0) <= 57) {
tempStr += score[i];
}else if(score[i].equals("*")) {
list.set((list.size()-1), (list.get(list.size()-1) * 2));
if(list.size() > 1) {
list.set((list.size()-2), (list.get(list.size()-2) * 2));
}
}
else if(score[i].equals("#")) {
list.set((list.size()-1), (list.get(list.size()-1) * (-1)));
}
else {
int tempInt = Integer.parseInt(tempStr);
switch(score[i]) {
case "S":
list.add(tempInt);
break;
case "D":
list.add((int)Math.pow(tempInt, 2));
break;
case "T":
list.add((int)Math.pow(tempInt, 3));
break;
}
tempStr = "";
}
}
for(int i=0; i<list.size(); i++) {
answer += list.get(i);
}
return answer;
}
}