본문 바로가기
알고리즘

[프로그래머스] 튜플

by SiO2whocode 2021. 5. 7.
반응형

programmers.co.kr/learn/courses/30/lessons/64065?language=cpp

 

코딩테스트 연습 - 튜플

"{{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

휴 잘풀었다 뿌듯

설명은 나중에,,

github.com/sio2whocodes/Programmers/blob/main/Programmers/%EC%88%98%EC%8B%9D%EC%B5%9C%EB%8C%80%ED%99%94.cpp

 

sio2whocodes/Programmers

프로그래머스 문제 풀이. Contribute to sio2whocodes/Programmers development by creating an account on GitHub.

github.com

#include <string>
#include <vector>
#include <algorithm>
#include <iostream>
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<int> in;
    vector<vector<int>> large;
    int num = 0;
    for(int i = 0 ; i < s.length() ; i++){
        if(s[i] == '{'){
            in.clear();
        }else if (s[i] == '}'){
            in.push_back(num);
            num = 0;
            large.push_back(in);
            i++;
        }else if(s[i] == ','){
            in.push_back(num);
            num = 0;
        }else{
            num = num*10 + s[i]-'0';
        }
    }
    sort(large.begin(), large.end(), cmp);
    for(int i = 0 ; i < large.size() ; i++){
        for(int j = 0 ; j < large[i].size() ; j++){
            int n = large[i][j];
            if(find(answer.begin(), answer.end(), n) == answer.end()){
                answer.push_back(n);
            }
        }
    }
    return answer;
}
반응형

댓글0