티스토리 뷰

내 풀이

오늘도 이중for문을 쓴 나 . . 조건문도 덕지덕지 ㅠ 

하 다른사람들 풀이를 보면 쉽게쉽게가는데 난 항상 어렵게간다 (반성)

거의 뭐 다른사람 풀이 보고싶어서 문제푸는느낌.. 

import java.util.*;

class Solution {
    public void insertHashMap(Map<String, String> map) {
        map.put("zero", "0");
        map.put("one", "1");
        map.put("two", "2");
        map.put("three", "3");
        map.put("four", "4");
        map.put("five", "5");
        map.put("six", "6");
        map.put("seven", "7");
        map.put("eight", "8");
        map.put("nine", "9");
    }
    
    public int solution(String s) {
        // s가 원래 의미하는 숫자
        int answer = 0;
        
        Map<String, String> map = new HashMap<String, String>();
        insertHashMap(map);
        
        StringBuffer buffer = new StringBuffer();
        StringBuffer result = new StringBuffer();
        
        for(int i=0; i<s.length(); i++) {
            if(Character.isDigit(s.charAt(i)) == false ) {
                buffer.append(s.charAt(i));
                if(map.containsKey(buffer.toString())) {
                    for(Map.Entry<String, String> entry : map.entrySet()) {
                        if(entry.getKey().equals(buffer.toString())) {
                            result.append(entry.getValue());
                            buffer.setLength(0);
                        }
                    }
                }
            } else if(Character.isDigit(s.charAt(i))) {
                result.append(s.charAt(i));
            }
        }
        
        answer = Integer.parseInt(result.toString());
        
        return answer;
    }
}

 

다른 풀이

 

import java.util.*;

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

        for(int i=0; i<10; i++){
        	if(s.contains(alpha[i]) {
            	s = s.replaceAll(alpha[i], Integer.toString(i));
            }
        }

        return Integer.parseInt(s);
    }
}

 

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/07   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
글 보관함