●문제



●간단한 설명
string s 가 있음
s는 one이나 1 이런식으로 숫자나 string으로 존재할수있음
값을 전부 숫자로 정리해서 return
●내가 푼 방법
unordred_map을 사용해서 0부터 9까지 맵을 생성
isdigit함수를 사용해서 문자면 그냥 값을 넣고
아니라면 map에 있는 값을 넣어줌
s[i]가 문자가 아닌경우 map에서 값을넣어준뒤 clear해서 다음숫자 체킹
마지막으로 구한 string값을 stoi로 int로 변형해서 return
●알아야하는 개념 / 알게된 개념
isdigit 은 문자? 라는 개념이다
ordered_map의 사용법
#include <string>
#include <vector>
#include <unordered_map>
#include <cctype>
using namespace std;
int solution(string s)
{
int answer = 0;
unordered_map<string, char> orderMap =
{
{"zero", '0'},
{"one", '1'},
{"two", '2'},
{"three", '3'},
{"four", '4'},
{"five", '5'},
{"six", '6'},
{"seven", '7'},
{"eight", '8'},
{"nine", '9'}
};
string result;
string temp;
for (int i = 0; i < s.size(); ++i)
{
//s[i]가 문자면?
if (isdigit(s[i]))
{
result += s[i];
}
else
{
temp += s[i];
}
//만약 orderMap에서 temp값을 찾으면
if (orderMap.find(temp) != orderMap.end())
{
result += orderMap[temp];
temp.clear();
}
}
answer = stoi(result);
return answer;
}

'C++ 프로그래머스 > 기타 문제' 카테고리의 다른 글
| 프로그래머스(C++) - 숫자의 표현 (0) | 2025.08.01 |
|---|---|
| 프로그래머스(C++) - JadenCase 문자열 만들기 (0) | 2025.08.01 |
| 프로그래머스(C++) - 콜라 문제 (0) | 2025.05.13 |
| 프로그래머스(C++) - 푸드 파이트 대회 (0) | 2025.05.12 |
| 프로그래머스(C++) - 시저 함수 (0) | 2025.05.07 |
