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;
}
'Coding_Test 연습 > Programmers' 카테고리의 다른 글
[프로그래머스] (C++) LV1 크레인 인형뽑기 게임 (0) | 2022.04.13 |
---|---|
[프로그래머스] (C++) LV1 키패드 누르기 (0) | 2022.04.13 |
[프로그래머스] (C++) LV1 신규 아이디 추천 (0) | 2022.04.12 |
[프로그래머스] (C++) LV1 로또의 최고 순위와 최저 순위 (0) | 2022.04.12 |
[프로그래머스] (C++) LV1 신고 결과 받기 (0) | 2022.04.12 |