프로그래머스: 튜플

danbibibi·2022년 7월 31일
0

문제

문제 바로가기> 프로그래머스: 튜플

풀이

반복문을 돌면서 집합의 길이와 원소를 저장한 후 정렬을 통해 가장 첫번째 원소 부터 answer에 넣어주었다. 이 때 중복 제거를 위해 set을 이용했다.

#include <string>
#include <vector>
#include <set>
#include <algorithm>
using namespace std;

vector<int> solution(string s) {
    vector<int> answer;
    
    set<int> check; // 중복 체크
    vector<pair<int, vector<int>>> lenAndNum;
    
    int len=0;
    string strnum="";
    vector<int> nums;
    
    for(int i=1; i<s.size()-1; i++){ // 집합의 길이와 원소 저장
        if(s[i]=='{') continue;
        else if(s[i]=='}'){
            nums.push_back(stoi(strnum)); // string to int
            strnum = "";
            lenAndNum.push_back({len, nums});
            len = 0;
            nums.clear();
        }
        else if(s[i]==','){
            if(s[i-1]=='}') continue;
            nums.push_back(stoi(strnum));
            len++;
            strnum = "";
        }
        else strnum+=s[i]; // 숫자
    }
    
    sort(lenAndNum.begin(), lenAndNum.end()); // 길이 오름차순 정렬
    
    for(auto lan: lenAndNum){ // 중복 원소인지 체크하면서 순서대로 넣어줌
        for(auto num: lan.second){
            if(check.count(num)!=0) continue;
            check.insert(num);
            answer.push_back(num);
        }
    }
    return answer;
}
profile
블로그 이전) https://danbibibi.tistory.com

0개의 댓글