Notice
Recent Posts
Recent Comments
송민준의 개발노트
프로그래머스-level1-다트게임 본문
https://programmers.co.kr/learn/courses/30/lessons/17682
코딩테스트 연습 - [1차] 다트 게임 | 프로그래머스
programmers.co.kr
나는 isNum을 만들어줬는데 Character의 메소드 중 isDigit가 숫자인지 아닌지 식별해준다고 한다... 참고^^
class Solution {
public int solution(String dartResult) {
int sum =0;
String[] jumsu = {"","",""};
int[] jumSqrt = {0, 0, 0};
int[] option = {1, 1, 1};
int jumsuIndex = 0;
for(int i =0; i < dartResult.length(); i++) {
String ch = Character.toString(dartResult.charAt(i));
if(isNum(ch)) {
jumsu[jumsuIndex] += ch;
} else {
if(ch.equals("S")) {
jumSqrt[jumsuIndex] = 1;
jumsuIndex++;
} else if(ch.equals("D")) {
jumSqrt[jumsuIndex] = 2;
jumsuIndex++;
} else if(ch.equals("T")){
jumSqrt[jumsuIndex] = 3;
jumsuIndex++;
} else if(ch.equals("*")) {
option[jumsuIndex-1] = 2;
if(jumsuIndex > 1) {
option[jumsuIndex-2] *= 2;
}
} else {
option[jumsuIndex-1] = -1;
}
}
}
for(int i=0; i < 3; i++) {
sum += Math.pow(Integer.parseInt(jumsu[i]),jumSqrt[i])*option[i];
}
return sum;
}
public static boolean isNum(String s) {
try {
Integer.parseInt(s);
return true;
} catch(NumberFormatException e) {
return false;
}
}
}
'알고리즘 > 프로그래머스' 카테고리의 다른 글
프로그래머스-level1-문자열을 정수로 바꾸기 (0) | 2019.11.18 |
---|---|
프로그래머스-level1- 문자열 다루기 기본 (0) | 2019.11.17 |
프로그래머스-level1-콜라츠 추측 (0) | 2019.11.11 |
프로그래머스-level1-핸드폰 번호 가리기 (0) | 2019.11.11 |
프로그래머스-level1-비밀지도 (0) | 2019.11.11 |