2021 카카오 채용연계형 인턴십
https://programmers.co.kr/learn/courses/30/lessons/81301#
코딩테스트 연습 - 숫자 문자열과 영단어
네오와 프로도가 숫자놀이를 하고 있습니다. 네오가 프로도에게 숫자를 건넬 때 일부 자릿수를 영단어로 바꾼 카드를 건네주면 프로도는 원래 숫자를 찾는 게임입니다. 다음은 숫자의 일부 자
programmers.co.kr
map을 사용해서 풀었다. find() 함수도 사용
#include <string>
#include <vector>
#include <unordered_map>
#include <iostream>
using namespace std;
int solution(string s) {
int answer = 0;
unordered_map<string, string> numbers ({
{"zero", "0"}, {"one", "1"}, {"two", "2"}, {"three", "3"}, {"four", "4"},
{"five", "5"}, {"six", "6"}, {"seven", "7"}, {"eight", "8"}, {"nine", "9"},
});
string temp = "";
string res = "";
for (int i=0; i<(int)s.size(); i++) {
if ('0' <= s[i] && s[i] <= '9') {
res += s[i];
temp = "";
}
else {
temp += s[i];
if (numbers.find(temp) != numbers.end()) {
res += numbers[temp];
temp = "";
}
}
}
answer = stoi(res);
return answer;
}
'ALGORITHM > OTHER' 카테고리의 다른 글
[Java] 16926 배열 돌리기 1, 16935 배열 돌리기 3 (0) | 2021.08.12 |
---|---|
[c++] 전화번호 목록 (프로그래머스-Trie, 정렬, 해시) (0) | 2021.07.17 |
[c++] 더 맵게 (프로그래머스 - heap/ priority queue) (0) | 2021.06.29 |
[c++] 실패율 (프로그래머스 - 구현) (0) | 2021.06.22 |
[c++] 신규 아이디 추천 (프로그래머스-구현) (0) | 2021.06.21 |