Jam's story
[숫자 문자열과 영단어] 본문
class Solution {
public int solution(String s) {
String[] mat= {"zero", "one", "two", "three","four","five", "six","seven" ,"eight","nine"};
for (int i = 0; i < mat.length; i++) {
s=s.replaceAll(mat[i],i+"");
}
int answer = Integer.parseInt(s);
return answer;
}
}
replaceAll함수는 문자열만 올 수 있기 때문에 i+""로 썻지만
Integer.toString(i)로 쓴다면 더 간단하게 해겴할 수 있다.
class Solution {
public int solution(String s) {
String[] mat= {"zero", "one", "two", "three","four","five", "six","seven" ,"eight","nine"};
for (int i = 0; i < mat.length; i++) {
s=s.replaceAll(mat[i],Integer.toString(i));
}
int answer = Integer.parseInt(s);
return answer;
}
}
이건 검색하다가 가져온 코드
class Solution {
public int solution(String s) {
String[][] mapArr = { {"0", "zero"},
{"1", "one"},
{"2", "two"},
{"3", "three"},
{"4", "four"},
{"5", "five"},
{"6", "six"},
{"7", "seven"},
{"8", "eight"},
{"9", "nine"} };
for(String[] map : mapArr){
s = s.replace(map[1], map[0]);
}
int answer = Integer.parseInt(s);
return answer;
}
}
'코딩테스트 > 프로그래머스' 카테고리의 다른 글
[숫자 문자열과 영단어] (0) | 2022.06.13 |
---|---|
[키패드 누르기] (0) | 2022.05.30 |
[신규 아이디 추천] (0) | 2022.05.27 |
[신고 결과 받기] (0) | 2022.05.26 |
[로또의 최고 순위와 최저 순위] (0) | 2022.05.26 |
Comments