숫자 문자열과 영단어

han.user();·2023년 3월 20일
0

프로그래머스

목록 보기
3/87
post-thumbnail

나의 코드

class Solution {
 public int solution(String s) {
  int answer = 0;
        
 String[] word = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
 String[] number = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"};

   for (int i = 0; i < word.length; i++) {
      s = s.replaceAll(word[i], number[i]);
   }

    answer = Integer.parseInt(s);
        
    return answer;
  }
}

영단어와 숫자 문자열의 배열을 각각 나열한다
zero가 있으면 0으로 바꾸고, five가 있으면 5로 바꿔서 String타입의 숫자열을 완성한다.
그 다음, 해당 숫자열을 int타입으로 바꾸고 리턴한다.

profile
I'm still hungry.

0개의 댓글