내 답
def solution(s):
dict={'zero':'0','one':'1','two':'2','three':'3','four':'4','five':'5','six':'6','seven':'7','eight':'8','nine':'9'}
result=''
temp=''
for i in s:
if i.isdigit():
result+=i
continue
temp+=i
if temp in dict:
result+=dict[temp]
temp=''
return int(result)
다른 사람의 답
num_dic = {"zero":"0", "one":"1", "two":"2", "three":"3", "four":"4", "five":"5", "six":"6", "seven":"7", "eight":"8", "nine":"9"}
def solution(s):
answer = s
for key, value in num_dic.items():
answer = answer.replace(key, value)
return int(answer)
자바 :
import java.util.*;
class Solution {
public int solution(String s) {
int answer = 0;
StringBuilder sb = new StringBuilder("");
int len = s.length();
String[] digits = {"0","1","2","3","4","5","6","7","8","9"};
String[] alphabets = {"zero","one","two","three","four","five","six","seven","eight","nine"};
for(int i=0; i<10; i++){
s = s.replaceAll(alphabets[i],digits[i]);
}
return Integer.parseInt(s);
}
}
'공부 > 코딩테스트' 카테고리의 다른 글
[프로그래머스] 나머지가 1이 되는 수 찾기(파이썬) (0) | 2021.12.10 |
---|---|
[프로그래머스] 부족한 금액 계산하기(파이썬) (0) | 2021.12.10 |
[프로그래머스] 스킬트리(파이썬) (0) | 2021.02.02 |
[프로그래머스] 기능개발(파이썬) (0) | 2021.01.26 |
[프로그래머스] 주식가격(파이썬) (0) | 2021.01.22 |