有希
HackerRank/Lonely Integer 본문
맨날 본 문제인듯?
set으로 해볼까 하다가 이러면 숫자를 집어넣을때마다 있으면 빼고 없으면 insert 하고(탐색 비용 소모)를 반복하고 다 돌고나서 set에 마지막 하나 남은 녀석이 홀수개(1개) 있는 녀석이므로 return 하면 되는데, 이러면 n * log n 이다.
map으로 하면 2n 돌리면 끝이니까 차라리 하던대로 map으로 했다.
int lonelyinteger(vector<int> a) {
map<int, int> nums;
int ret = 0;
for(int i=0; i<a.size(); ++i)
{
nums[a[i]]++;
}
for(auto i : nums)
{
if(i.second == 1) ret = i.first;
}
return ret;
}
'프로그래밍 > 알고리즘+코딩테스트' 카테고리의 다른 글
HackerRank/Counting Sort 1 (0) | 2022.03.29 |
---|---|
HackerRank/Diagonal Difference (0) | 2022.03.29 |
HackerRank/Mini-Max Sum (0) | 2022.03.29 |
C++/문자열에서 앞 뒤 공백 제거, 문자열에서 공백을 없애고 split하기 (0) | 2022.03.29 |
HackerRank/Plus Minus (0) | 2022.03.29 |