map을 사용하면 크게 어렵지 않았던 문제다.

원래는 python으로 카카오 코테 쳤었는데 C++로 다시 풀어 봤다.

숫자인것과 아닌것을 나눠서 string으로 저장한 후, stoi로 변환해줬다.

#include <string>
#include <vector>
#include <map>

using namespace std;

int solution(string s) {
    int answer = 0;

    string temp = "";
    string out = "";
    map<string, string> num;
    num["zero"] = "0";
    num["one"] = "1";
    num["two"] = "2";
    num["three"] = "3";
    num["four"] = "4";
    num["five"] = "5";
    num["six"] = "6";
    num["seven"] = "7";
    num["eight"] = "8";
    num["nine"] = "9";

    for (int i = 0; i < s.length(); i++)
    {
        if (isdigit(s[i]) == 0)
        {
            temp += s[i];
            if (num[temp] != "")
            {
                out += num[temp];
                temp = "";
            }
        }
        else
            out += s[i];
    }

    answer = stoi(out);

    return answer;
}

+ Recent posts