https://programmers.co.kr/learn/courses/30/lessons/64065
코딩테스트 연습 - 튜플
"{{2},{2,1},{2,1,3},{2,1,3,4}}" [2, 1, 3, 4] "{{1,2,3},{2,1},{1,2,4,3},{2}}" [2, 1, 3, 4] "{{4,2,3},{3},{2,3,4,1},{2,3}}" [3, 2, 4, 1]
programmers.co.kr
#include <string>
#include <vector>
#include <algorithm>
#include <sstream>
using namespace std;
bool arange(string n1, string n2) {
return n1.size() < n2.size();
}
vector<int> solution(string s) {
vector<int> answer;
vector<string> list;
stringstream ss(s.substr(1,s.size()-2));
string tmp;
getline(ss, tmp, '}');
list.push_back(tmp.substr(1, tmp.size() - 1));
while (getline(ss, tmp, '}')) {
list.push_back(tmp.substr(2, tmp.size() - 1));
}
sort(list.begin(), list.end(), arange);
for (string str : list) {
stringstream ss(str);
string tmp;
while (getline(ss, tmp, ',')) {
if (find(answer.begin(), answer.end(), stoi(tmp)) == answer.end()) {
answer.push_back(stoi(tmp));
}
}
}
return answer;
}
'전공공부 > 코딩테스트' 카테고리의 다른 글
(c++) 프로그래머스 "리틀 프렌즈" (0) | 2022.03.28 |
---|---|
(c++) [미완성] "브라이언의 고민" (0) | 2022.03.28 |
(c++) 프로그래머스 "3진법 뒤집기" (0) | 2022.03.15 |
(c++) 프로그래머스 "수식 최대화" (0) | 2022.03.15 |
(c++) 프로그래머스 "약수의 개수와 덧셈" (0) | 2022.03.14 |