알고리즘테스트/Programmers
[Lv.1_Java]숫자 문자열과 영단어
코딩거인
2023. 8. 18. 16:54
728x90
class Solution {
public int solution(String s) {
String[] num = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"};
String[] word = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
for (int i = 0; i < 10; i++) {
s = s.replace(word[i], num[i]);
}
return Integer.valueOf(s);
}
}
String 형으로 주어졌기 때문에 num 과 word 라는 배열을 만든 뒤 for 문을 활용하여 replace 를 해준다음 Integer.valueOf() 함수를 사용해서 int 형으로 형변환 시켜준뒤 return 하였다!

728x90