[Programmers] 튜플

Date:

[Programmers] 튜플

Problem URL : 튜플

#include <string>
#include <vector>
#include <algorithm>
#include <set>

using namespace std;

bool cmp(vector<int> a, vector<int> b) {
    return a.size() < b.size();
}

vector<int> solution(string s) {
    vector<int> answer;
    vector<vector<int>> v;
    int size = s.size();
    vector<int> input;
    string tmp = "";
    bool open = false;
    for (int i = 1; i < size - 1; i++) {
        if (s[i] == '{') {
            open = true;
        }
        if (s[i] >= '0' && s[i] <= '9') {
            tmp += s[i];
        }
        if (s[i] == ',') {
            if (open) {
                input.push_back(stoi(tmp));
                tmp = "";
            }
        }
        if (s[i] == '}') {
            input.push_back(stoi(tmp));
            tmp = "";
            v.push_back(input);
            input.clear();
            open = false;
        }
    }

    sort(v.begin(), v.end(), cmp);
    
    set<int> tuple;
    for (int i = 0; i < v.size(); i++) {
        for (int j = 0; j < v[i].size(); j++) {
            if (tuple.find(v[i][j]) == tuple.end()) {
                tuple.insert(v[i][j]);
                answer.push_back(v[i][j]);
            }
        }
    }

    return answer;
}

댓글