-
[프로그래머스/해시/Lv.1] 완주하지 못한 선수프로그래머스/해시 2020. 6. 25. 21:25
문제 설명
코드1 : 정렬
#include <string> #include <vector> #include <algorithm> using namespace std; string solution(vector<string> participant, vector<string> completion) { string answer = ""; sort(participant.begin(),participant.end()); sort(completion.begin(),completion.end()); for(int i=0;i<completion.size();i++){ if(completion[i]!=participant[i]){ answer=participant[i]; break; } } return answer; }
코드2 : 해시
#include <string> #include <vector> #include <unordered_map> using namespace std; string solution(vector<string> participant, vector<string> completion) { string answer = ""; unordered_map<string,int> umap; for(auto i : participant){ //umap.insert(make_pair(i,1)); umap[i]++; } for(auto i : completion){ umap[i]--; } for(auto i : umap){ if(i.second==1){ answer=i.first; } } return answer; }
소감
첫째로 unordered_map pair의 second를 int형으로 지정할 경우 0으로 초기화 된다는 사실을 배울 수 있었다.
또한 sort나 search 등 복잡한 절차 없이도 중복 처리를 할 수 있다는 작지만 소중한 아이디어를 얻었다.'프로그래머스 > 해시' 카테고리의 다른 글
[프로그래머스/해시/Lv.3] 베스트앨범 (0) 2020.06.25 [프로그래머스/해시/Lv.2] 위장 (0) 2020.06.25 [프로그래머스/해시/Lv.2] 전화번호 목록 (0) 2020.06.25