프로그래머스
[프로그래머스/c++] Lv2 튜플
돈 많은 백수가 되고싶다
2024. 12. 15. 23:33
https://school.programmers.co.kr/learn/courses/30/lessons/64065
프로그래머스
SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프
programmers.co.kr
1. 문자열에서 숫자 추출하기 (isdigit 사용)
2. 한자리수만 있는거 아니니까 고려하기
3. 추출한 숫자 unordered_map에 넣기
4. 벡터 만들어서 map에 있는 것 넣기
5. 정렬해서 answer에 넣기
#include <string>
#include <vector>
#include <cctype>
#include <algorithm>
#include <unordered_map>
using namespace std;
vector<int> solution(string s) {
vector<int> answer;
int num = 0;
bool check = false;
unordered_map<int, int> freq;
for (int i = 0; i < s.size(); i++) {
//숫자 추출
if (isdigit(s[i])) {
num = num * 10 + (s[i] - '0');
check = true;
}
else if (check) {
freq[num]++;
num = 0;
check = false;
}
}
vector<pair<int, int>> vFeq(freq.begin(), freq.end());
sort(vFeq.begin(), vFeq.end(), [](pair<int, int>a, pair<int, int>b) {
return a.second > b.second;
});
for (auto& [num, count] : vFeq) {
answer.push_back(num);
}
return answer;
}