알고리즘/프로그래머스
[2019 카카오 개발자 겨울 인턴십 코딩테스트] 문제2. 튜플
동 코
2020. 4. 3. 17:58
문제풀이
코딩 테스트에 자주 사용되는 string_tokenize를 사용하는 문제입니다.
그리고 분리한 배열들의 순서를 정하는 아이디어는 길이를 이용하여 소팅하는 방법을 이용하였습니다.
문제자체는 어렵지 않지만 이미 만들어본 string_tokenize를 적용하려다보니 1,2,3문제중에서는 가장 좀 지저분하게 풀었고 시간을 쓴 문제였습니다.
소스코드
더보기
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
#include <string>
#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
using namespace std;
bool cmp(string s1, string s2) {
}
vector<string> string_tokenize(string str, string delimiter) {
vector<string> token;
vector<int> del_pos;
//token.push_back(str.substr(0, str.find(" ")));
del_pos.push_back(0);
for (int i = 0; i < str.size(); i++) {
del_pos.push_back(index + 1);
i = index + 1;
}
else {
del_pos.push_back(str.size() + 1);
break;
}
}
int del_pos_size = del_pos.size();
for (int i = 0; i < del_pos_size - 1; i++) {
}
del_pos.pop_back();
del_pos_size = del_pos.size();
}
return token;
}
vector<int> solution(string s) {
vector<int> answer;
set<int> set_num;
s = s.substr(1);
vector<string> s1 = string_tokenize(s, "}");
s1.pop_back();
for (int i = 0; i < s1.size(); i++) {
//cout << s1[i] << endl;
vector<string> temp = string_tokenize(s1[i], "{");
s1[i] = temp[1];
//cout << s1[i] << endl;
}
//길이별로 sort
sort(s1.begin(), s1.end(), cmp);
cout << s1[0] << endl;
answer.push_back(stoi(s1[0]));
for (int i = 1; i < s1.size(); i++) {
vector<string> s2 = string_tokenize(s1[i], ",");
for (int i = 0; i < s2.size(); i++) {
int num = stoi(s2[i]);
answer.push_back(num);
}
}
}
return answer;
}
int main() {
solution("{{2},{2,1},{2,1,3},{2,1,3,4}}");
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
|
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs |
문제링크 : https://programmers.co.kr/learn/courses/30/lessons/64065
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr