Level 0. 글자 지우기
indices의 원소 범위가 주어지지 않아. 배열 대신 unordered_map을 사용했다.
문자열을 붙이게 되면 메모리 재할당이 빈번하게 발생하므로, 미리 크기를 할당해놓고 사용했다.
#include <iostream>
#include <vector>
#include <unordered_map>
using namespace std;
string solution(string my_string, vector<int> indices)
{
size_t len = my_string.length(), size = indices.size();
string answer(len - size, '\0');
unordered_map<int, bool> umap;
for (int index : indices)
umap[index] = true;
for (int i = 0, j = 0; j < answer.length(); ++i)
if (umap[i] == false)
answer[j++] = my_string[i];
return answer;
}
'자료구조 & 알고리즘 > 프로그래머스' 카테고리의 다른 글
Level 2. 구명보트 (1) | 2023.05.20 |
---|---|
Level 2. N개의 최소공배수 (0) | 2023.05.19 |
Level 0. 문자열이 몇 번 등장하는지 세기 (0) | 2023.05.17 |
Level 0. 문자열 겹쳐쓰기 (0) | 2023.05.16 |
Level 0. 수열과 구간 쿼리 2 (0) | 2023.05.15 |