Algorithm/문제 풀이
프로그래머스 - 숫자 문자열과 영단어
개발자-H
2021. 8. 8. 15:50
https://programmers.co.kr/learn/courses/30/lessons/81301
코딩테스트 연습 - 숫자 문자열과 영단어
네오와 프로도가 숫자놀이를 하고 있습니다. 네오가 프로도에게 숫자를 건넬 때 일부 자릿수를 영단어로 바꾼 카드를 건네주면 프로도는 원래 숫자를 찾는 게임입니다. 다음은 숫자의 일부 자
programmers.co.kr
- 문자열 치환 문제
- 설마 이게 되겠어 하고 제출했더니 그냥 통과가 되었다.
- 테케가 빡빡해서 시간 초과 날줄 알았는데. replace 함수로 해결이 되었다.
class Solution {
public int solution(String s) {
String answer = s.replace("one", "1")
.replace("two", "2")
.replace("three", "3")
.replace("four", "4")
.replace("five", "5")
.replace("six", "6")
.replace("seven", "7")
.replace("eight", "8")
.replace("nine", "9")
.replace("zero", "0");
return Integer.parseInt(answer);
}
}