Algorithm/프로그래머스

[Level 1] 숫자 문자열과 영단어

녱녱 2023. 7. 23.

class Solution {
    public int solution(String s) {
        String[] arr = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};

        for(int i=0;i<10;i++) {
            s = s.replace(arr[i], Integer.toString(i));
        }

        return Integer.parseInt(s);
    }
}

1. 숫자에 대응 하는 영단어 배열 arr을 생성 (숫자가 0~9까지로 정해짐)

2. 반복문을 사용해 배열 안의 단어를 돌며 replace 메소드를 사용해 해당 영단어를 숫자로 변경한다

3. Int 형을 반환해야 하므로 String 형으로 나온 결과값을 Int 형으로 반환하도록 한다

댓글