๐ฃ ๋ฌธ์ ์ดํด
ํน์ ํํ์ ํํํ๋ ์งํฉ์ด ๋ด๊ธด ๋ฌธ์์ด s๊ฐ ๋งค๊ฐ๋ณ์๋ก ์ฃผ์ด์ง ๋,
s๊ฐ ํํํ๋ ํํ์ ๋ฐฐ์ด์ ๋ด์ return ํ๋๋ก solution ํจ์๋ฅผ ์์ฑํด์ผ ํ๋ค.
์ ์ถ๋ ฅ ์
s | result |
"{{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] |
"{{20,111},{111}}" | [111, 20] |
"{{123}}" | [123] |
"{{4,2,3},{3},{2,3,4,1},{2,3}}" | [3, 2, 4, 1] |
๐ญ ํ์ด ๊ณผ์
๋ฌธ์์ด ํ์ฑ์ ๋ฌผ์ด๋ณด๋ ๋ฌธ์ ์๋ค. C++๋ก ํ์ด๋ณด๋ คํ๋ ๋์ด๋์ ๋นํด ์์ค ๊ธธ์ด๊ฐ ์งง์ ํธ์ ์๋์๋ค.
๊ตฌ๊ธ๋งํ๋ฉด์ ํ์ด์ฌ์ผ๋ก ์์ฑํ ๋ค๋ฅธ ์์ค์ฝ๋๋ค์ ๋ณด๋ ๊ทธ์ ๋น..๐ค๐ผ
์์ฑ ์ธ์ด: C++
#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
using namespace std;
vector<int> solution(string s) {
vector<int> answer;
vector<int> array;
vector<pair<int, vector<int>>> vt;
int size = s.size();
int num = 0;
for (int i = 1; i < size; ++i) {
if (s[i] == '{') continue;
if (s[i] == '}') {
if (s[i-1] == '}') continue;
array.push_back(num);
vt.push_back(make_pair(array.size(), array));
num = 0;
array.clear();
}else if (s[i] == ',') {
if (s[i-1] == '}') continue;
array.push_back(num);
num = 0;
}else {
num *= 10;
num += s[i] - '0';
}
}
sort(vt.begin(), vt.end());
set<int> st;
for (pair<int, vector<int>>p:vt) {
for (int num:p.second) {
if (st.find(num) == st.end()) { // set์ ์ถ๊ฐ๋ ์ ์๋ ์์
st.insert(num);
answer.push_back(num);
}
}
}
return answer;
}
๐ ๋ฐฐ์ด ์
๋ฌผ๋ก C, C++ ์๋๊ฐ ํ ์ธ์ด์ ๋นํด ๋น ๋ฅธ ํธ์ด๋ผ๊ณ ํ์ง๋ง,
๋ฌธ์์ด ํ์ฑ ๋ฌธ์ ์์ ํ์ด์ฌ์ผ๋ก ์์ฑํ ๋ค๋ฅธ ํ์ด๋ค์ 10์ค? ์ด๋ด๋ก ํ์ด๋ด๋ ๊ฑธ ๋ณด๊ณ ๊ฐํํ๋ค..
์ด๋ฐ ๋ฌธ์ ๋ฅ ํ์ด๋ฅผ ์ํด ํ์ด์ฌ์ผ๋ก ํธ๋ ๊ฒ๋ ์ข ์ฐ์ตํด๋ด์ผํ๋.. ์ถ์ ์๊ฐ์ด ๋ค์๋ค.
์๋ฌดํผ ๋์ด๋๋ ๋๋ฆ ์ค๋ฌด์ค...???