[프로그래머스] 숫자 문자열과 영단어 [문자열] - c++

ha·2022년 2월 13일
0

프로그래머스

목록 보기
21/21

https://programmers.co.kr/learn/courses/30/lessons/81301

c++ 풀이(map)

#include <string>
#include <vector>
#include <unordered_map>
using namespace std;

int solution(string s) {
    int answer = 0;
    unordered_map <string,char> mp;
    mp["zero"]='0';mp["one"]='1';mp["two"]='2';mp["three"]='3';mp["four"]='4';mp["five"]='5';
    mp["six"]='6';mp["seven"]='7';mp["eight"]='8';mp["nine"]='9';
    
    string ans = "";
    int idx=0;
    while(idx<s.size())
    {
        if(s[idx]>='0' && s[idx]<='9')
        {
            ans.push_back(s[idx]);
            idx++;
        }
        else
        {
            string tmp="";
            while(s[idx]>='a'&&s[idx]<='z')
            {
                tmp.push_back(s[idx++]);
                if(mp[tmp]>='0' && mp[tmp]<='9') break;
            }
            ans.push_back(mp[tmp]);
        }
    }
    answer=stoi(ans);
    return answer;
}

0개의 댓글