์ฒด๊ฐ Level : โ
โ
โ
Review: ์ ๊ท์๊ณผ ํจํด์ ์ฌ์ฉํด์ผํ๋ ๋ฌธ์ ... ํจํด ๋ฝ์์ค๋ ๋ถ๋ถ๋ง ์ฐธ๊ณ ํ๊ณ ๋๋จธ์ง๋ ์ง์ ์ฝ๋ฉํ๋๋ฐ ์กฐ๊ฑด์ด ๊น๋ค๋ก์์ ์๊ฐ์ด ์ค๋ ๊ฑธ๋ ธ๋ค ใ ใ |
๐ก
๋์๋ฆฌ ์๋ฅผ ๊ฐ์ ธ์์ผํ๋ ์
* ๊ฐ ๋์ค๋ฉด ์ด์ ๊ฐ๋ *2 ํด์ค์ผํ๋๊ฒ.
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
class Solution {
public int solution(String dartResult) {
int answer = 0;
String[] arr = new String[3];
String ptn = "(\\d+)([SDT])([*#]?)";
Pattern p = Pattern.compile(ptn);
Matcher m = p.matcher(dartResult);
int j = 0;
int []result = new int[3];
while (m.find()) {
arr[j] = m.group();
j++;
}
int i =0;
for (String s : arr){
int num = 0;
int tmp = 0;
// * ๋ง๋๋ฉด ๊ทธ์ ๊บผ ๋๋ฐฐ
if(s.contains("S")){
if(s.contains("10")){
num = Integer.valueOf("10");
}else {
num = Integer.valueOf(s.substring(0,1));
}
result[i] += Math.pow(num,1); // 1^1
if (s.contains("*")){
result[i] *= 2;
if (i != 0){
result[i-1] *= 2;
}
}else if (s.contains("#")){
result[i] *= (-1);
}
i++;
}else if(s.contains("D")){
if(s.contains("10")){
num = Integer.valueOf("10");
}else {
num = Integer.valueOf(s.substring(0,1));
}
result[i] += Math.pow(num,2);
if (s.contains("*")){
result[i] *= 2;
if (i != 0){
result[i-1] *= 2;
}
}else if (s.contains("#")){
result[i] *= (-1);
}
i++;
}else if(s.contains("T")){
if(s.contains("10")){
num = Integer.valueOf("10");
}else {
num = Integer.valueOf(s.substring(0,1));
}
result[i] += Math.pow(num,3);
if (s.contains("*")){
result[i] *= 2;
if (i != 0){
result[i-1] *= 2;
}
}else if (s.contains("#")){
result[i] *= (-1);
}
i++;
}
}
for (int n : result){
answer += n;
}
return answer;
}
}