[프로그래머스/Java] Lv.1 숫자 문자열과 영단어

이은정·2024년 10월 2일

프로그래머스/Java

목록 보기
49/74

문제

로직

영단어 리스트를 문자열 배열로 저장한 후에 replace() 함수를 이용하여 각 영단어에 맞는 숫자로 변환한다.

코드

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);
    }
}

결과

profile
돈 많은 백수가 꿈인 백엔드 개발자 지망생

0개의 댓글