문제 설명
과정
- 각 숫자에 대응되는 영단어와 숫자를 맵핑할 문자열 배열
- 주어진 문자열 s에 대해, 각 영단어를 숫자로 대체
- 치환이 완료된 s는 정수로 변환
최종코드
class Solution {
public int solution(String s) {
String[] words = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
for (int i = 0; i < words.length; i++) {
s = s.replace(words[i], Integer.toString(i));
}
return Integer.parseInt(s);
}
}
- 참고할 다른 풀이 방법
더보기
replaceAll을 사용
class Solution {
public int solution(String s) {
String[] strArr = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
for(int i = 0; i < strArr.length; i++) {
s = s.replaceAll(strArr[i], Integer.toString(i));
}
return Integer.parseInt(s);
}
}
프로그래머스
SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프
programmers.co.kr
'개발 공부 > 코딩테스트' 카테고리의 다른 글
99클럽 코테 스터디 8일차 TIL - 해시 (0) | 2024.11.05 |
---|---|
99클럽 코테 스터디 5일차 TIL - 해시 (0) | 2024.11.01 |
99클럽 코테 스터디 3일차 TIL - 문자열 (0) | 2024.10.31 |
99클럽 코테 스터디 2일차 TIL - 문자열 (1) | 2024.10.30 |
99클럽 코테 스터디 1일차 TIL - 문자열 (0) | 2024.10.29 |