hash; for (auto participant : participants) { hash[participant] += 1; } for (auto completion : completions) { hash[completion] -= 1; } for (auto participant : participants) { if (hash[participant] == 1) answer = participant; } return answer; }"> hash; for (auto participant : participants) { hash[participant] += 1; } for (auto completion : completions) { hash[completion] -= 1; } for (auto participant : participants) { if (hash[participant] == 1) answer = participant; } return answer; }"> hash; for (auto participant : participants) { hash[participant] += 1; } for (auto completion : completions) { hash[completion] -= 1; } for (auto participant : participants) { if (hash[participant] == 1) answer = participant; } return answer; }">
#include <string>
#include <iostream>
#include <vector>
#include <unordered_map>
using namespace std;
string solution(vector<string> participants, vector<string> completions) {
string answer = "";
unordered_map<string, int> hash;
for (auto participant : participants) {
hash[participant] += 1;
}
for (auto completion : completions) {
hash[completion] -= 1;
}
for (auto participant : participants) {
if (hash[participant] == 1)
answer = participant;
}
return answer;
}
다른풀이
#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(participant[i] != completion[i])
return participant[i];
}
return participant[participant.size() - 1];
//return answer;
}