-
[Programmers] Lv2. ํํSW Test/Programmers 2020. 9. 1. 13:21๋ฐ์ํ
- ์ด ๊ธ์ C++๋ก ํ์ด๋ฅผ ์์ฑํ์์ต๋๋ค.
๋ฌธ์
์์
ํ์ด
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters#include <string> #include <vector> #include <algorithm> using namespace std; /** {{1,2,3},{2,1},{1,2,4,3},{2}} ์ด ๋ฌธ์์ด์ ๊ฐ์ง๊ณ ์์์ ๊ฐฏ์๊ฐ ์ ์ ์์๋๋ก ๋ฆฌ์คํธ๋ฅผ ์ ๋ ฌํ๊ธฐ ์ํด์ ๋น๊ตํจ์ ๊ตฌํ {2},{2,1},{1,2,3},{1,2,4,3} ์์ผ๋ก ๊ฒฐ๊ณผ ์ถ๋ ฅ **/ bool cmp(vector<int> a, vector<int> b){ return a.size()<b.size(); } vector<int> solution(string s) { vector<vector<int>> v; // ๋ฌธ์์ด์ ๊ฐ์ง๊ณ 2์ค ๋ฐฐ์ด ๊ตฌํ vector<int> answer; bool flag = false; // '{' ๊ดํธ์ดํ ์ฌ๋ถ vector<int> temp; // ๋ฌธ์์ด์ ๋ฐฐ์ด๋ก ๋ง๋ค ๋ ์ฐ์ด๋ ์์๋ฐฐ์ด string num = ""; // ์ซ์๋ฅผ ํํํ ๋ฌธ์์ด // s์ ์ฒ์๊ณผ ๋์ ํ์ธํ ํ์์๋ค. for(int i=1;i<s.size()-1;i++){ if(s[i] == '{'){ // '{' ๊ดํธ๊ฐ ๋์ค๋ฉด ์์ flag = true; } else if(isdigit(s[i])){ // ์ซ์์ผ ์ num ๋ฌธ์์ด์ ์ถ๊ฐ, ์ซ์๋ ๋ฌด์กฐ๊ฑด flag๊ฐ true์ผ ๋๋ง ๊ฐ๋ฅ num+=s[i]; } else if(flag && s[i] == ','){ // ์ซ์๋ฅผ ์์๋ฐฐ์ด์ ๋ฃ๊ธฐ ์ ํ์ธ temp.push_back(stoi(num)); // ์์๋ฐฐ์ด์ ์ซ์ ๋ฃ๊ธฐ num.clear(); // ์ซ์ ์ด๊ธฐํ } else if(s[i]=='}'){ // '}' ๊ดํธ๊ฐ ๋์ฌ๊ฒฝ์ฐ ํ๋์ ๋ฐฐ์ด ์์ฑ ์ข ๋ฃ, 2์ค๋ฐฐ์ด์ ๋ฃ๊ธฐ temp.push_back(stoi(num)); v.push_back(temp); num.clear(); temp.clear(); flag=false; } } sort(v.begin(),v.end(),cmp); // ์ ๋ ฌ // ์์์ ๊ฐฏ์๊ฐ ์ ์ ๋ฐฐ์ด๋ถํฐ ๊ฒ์ฌ for(int i=0;i<v.size();i++){ for(int j=0;j<v[i].size();j++){ if(find(answer.begin(),answer.end(),v[i][j])==answer.end()){ // ์ค๋ณต ์ ๊ฑฐ answer.push_back(v[i][j]); } } } return answer; } - ์ถ๊ฐ๋ก ๊ถ๊ธํ ์ ์ด๋ ์์ ํ ๋ถ๋ถ ์์ผ๋ฉด ๋๊ธ๋ก ๋จ๊ฒจ์ฃผ์ธ์.
๋ฐ์ํ'SW Test > Programmers' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Programmers] Lv3. N์ผ๋ก ํํ (0) 2020.09.02 [Programmers] Lv2.์์ ์ต๋ํ (0) 2020.09.01 [Programmers] Lv1. ํคํจ๋ ๋๋ฅด๊ธฐ (0) 2020.08.31 [Programmers] Lv.1 ํฌ๋ ์ธ ์ธํ๋ฝ๊ธฐ (0) 2020.08.31 [Programmers] Lv3. ๋ฐฉ๋ฌธ ๊ธธ์ด (0) 2020.05.03